Image resize in php

Here there are some basic help for image resizing code in php


function get_width($mages) {
    list($img_width, $img_height) = getimagesize($mages);
    return $img_width;
}

function get_height($mages) {
    list($img_width, $img_height) = getimagesize($mages);
    return $img_height;
}

function get_ratio($mages) {
    list($img_width, $img_height) = getimagesize($mages);
    return $img_width / $img_height;
}

function get_type($mages) {
    list($img_width, $img_height, $type, $attr) = getimagesize($mages);
    return $type;
}

function get_mime($mages) {
    $array = getimagesize($mages);
    return $array['mime'];
}

function resize($width, $height, $img) {

    $old_image = imagecreatefromjpeg($img);
    $image_width = imagesx($old_image);
    $image_height = imagesy($old_image);
    $image_aspect_ratio = $image_width / $image_height;
    $image_type = get_type($img);
    $image_mime = get_mime($img);

    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
    if(file_exists('resized-imges/sample2.jpg'))
        unlink('resized-imges/sample2.jpg');
    imagejpeg($new_image, 'resized-imges/sample2.jpg', 100);

    imagedestroy($new_image);
}

function resizeToHeight($height, $img) {

    $old_image = imagecreatefromjpeg($img);
    $image_width = imagesx($old_image);
    $image_height = imagesy($old_image);
    $ratio = $height / $image_height;
    $width = $image_width * $ratio;
    resize($width, $height, $img);
}

function resizeToWidth($width, $img) {
    $old_image = imagecreatefromjpeg($img);
    $image_width = imagesx($old_image);
    $image_height = imagesy($old_image);
    $ratio = $width / $image_width;
    $height = $image_height * $ratio;
    resize($width, $height, $img);
}

$img = ('images/Shrek2-3.jpg');

resizeToWidth(600, $img);

Tagged: , ,

One thought on “Image resize in php

  1. Frieda October 30, 2012 at 12:02 pm Reply

    This is a message to the webmaster. Your website:https://midpart.wordpress.com/2012/07/14/image-resize-in-php/ is missing out on at least 300 visitors per day. I came to this page via Google but it was difficult to find as you were not on the front page of search results. I have found a website which offers to dramatically increase your traffic to your website: http://voxseo.com/traffic/. I managed to get over 10,000 visitors per month using their services, you could also get lot more targeted traffic than you have now. Hope this helps 🙂 Take care.

Leave a comment