1

Having an issue with a image resize function.

Function

// Image Resizer
function MakeThumbnail($inputFile, $filepath, $ext, $maxWidth, $maxHeight){
    /* Get some details about the image */
    $srcDetails = getimagesize($inputFile);
    switch ($srcDetails[2]) {
        case 1: //GIF
            $source_image = imagecreatefromgif($inputFile);
            break;          
        case 2: //JPEG
            $source_image = imagecreatefromjpeg($inputFile);
            break;          
        case 3: //PNG
            $source_image = imagecreatefrompng($inputFile);
            break;          
        case 6: //WBMP
            $source_image = imagecreatefromwbmp($inputFile);
            break;          
        default:
            break;
    }       
    /* Original Dimensions */
    $width = imagesx($source_image);
    $height = imagesy($source_image);       
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
    if(($maxWidth <= $width) && $maxHeight <= $height){
        if($target_ratio == $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $maxWidth;
        }elseif($target_ratio > $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $width * ($maxHeight / $height);
        }elseif($target_ratio < $original_ratio){
            $desired_height = $height * ($maxWidth / $width);
            $desired_width = $maxWidth;
        }
    }else{
        $desired_height = $maxHeight;
        $desired_width = $maxWidth;
    }
imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);        
    /* create the physical thumbnail image to its destination */
    switch ($srcDetails[2]) {
        case 1: //GIF
            imagegif($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        case 2: //JPEG
            imagejpeg($virtual_image, $filepath, 100);
            imagedestroy($virtual_image);
            break;          
        case 3: //PNG
            imagepng($virtual_image, $filepath, 6);
            imagedestroy($virtual_image);
            break;          
        case 6: //WBMP
            imagewbmp($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        default:
            imagedestroy($virtual_image);
            break;          
    }
}

Usage

MakeThumbnail($pic2['tmp_name'], $pic2Path, $extension, 800, 600); //<- resize original
MakeThumbnail($pic2['tmp_name'], $pic2ThumbPath, $extension, 150, 100); //<- make a thumbnail

Image resizing with the ratio now works with the updated function above. Issue I am finding now, is that the image(s) are resized up (made bigger), if the original width is smaller that what I am trying to specify.

What I am after is a resized image that will never be wider than $maxWidth, never be taller than $maxHeight, yet keep the width to height ratio of the original image.

Community
  • 1
  • 1
Kevin
  • 2,684
  • 6
  • 35
  • 64
  • What are you trying to do here? `$newwidth = $newheight = min($size, max($width, $height)); ` – Digital Chris Dec 20 '13 at 16:23
  • I mean what are you attempting with that line of code I quoted; I think it's the source of your trouble. – Digital Chris Dec 20 '13 at 16:43
  • I found the original code you inherited here in your previous question: http://stackoverflow.com/questions/20707086/image-resize-on-upload-issue. It already resizes while maintaining aspect ratio, just put 0 in for the MaxHeight value. – Digital Chris Dec 20 '13 at 17:00
  • What do you mean "no sorry it did not?" I'm looking at the code, and if you put 0 in as maxHeight, it calculates height based on the aspect ratio. Its right here `$newheight = (strlen($maxHeight)>0) ? $maxHeight : ($height / $width) * $newwidth;` – Digital Chris Dec 20 '13 at 17:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43612/discussion-between-digital-chris-and-o7th-web-design) – Digital Chris Dec 20 '13 at 17:12
  • I would not reinvent the wheel. Try https://github.com/avalanche123/Imagine for image manipulation it comes with unit tests as well. – floriank Dec 20 '13 at 19:45

2 Answers2

1

You want this I think:

    $width = imagesx($source_image);
    $height = imagesy($source_image);     
    // new code to check if image is too small   
    if ($width <= $maxWidth || height <= $maxHeight) { //because we need to check either, and not both
     // image is too small; don't resize
        $doResize = false;
    } else {
        $doResize = true;
    }

Then around your imagecopyresized put:

if ($doResize) {
     imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);        
}
Kevin
  • 2,684
  • 6
  • 35
  • 64
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
0

Got it.

    /* Get some details about the image */
    $srcDetails = getimagesize($inputFile);
    switch ($srcDetails[2]) {
        case 1: //GIF
            $source_image = imagecreatefromgif($inputFile);
            break;          
        case 2: //JPEG
            $source_image = imagecreatefromjpeg($inputFile);
            break;          
        case 3: //PNG
            $source_image = imagecreatefrompng($inputFile);
            break;          
        case 6: //WBMP
            $source_image = imagecreatefromwbmp($inputFile);
            break;          
        default:
            break;
    }       
    /* Original Dimensions */
    $width = imagesx($source_image);
    $height = imagesy($source_image);   
    $target_ratio = $maxWidth / $maxHeight;
    $original_ratio = $width / $height; 
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
    if(($maxWidth <= $width) || $maxHeight <= $height){ // <-- needed to change this to orelse
        if($target_ratio == $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $maxWidth;
        }elseif($target_ratio > $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $width * ($maxHeight / $height);
        }elseif($target_ratio < $original_ratio){
            $desired_height = $height * ($maxWidth / $width);
            $desired_width = $maxWidth;
        }
    }else{
        $desired_height = $maxHeight;
        $desired_width = $maxWidth;
    }
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);     
    imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);      
    /* create the physical thumbnail image to its destination */
    switch ($srcDetails[2]) {
        case 1: //GIF
            imagegif($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        case 2: //JPEG
            imagejpeg($virtual_image, $filepath, 100);
            imagedestroy($virtual_image);
            break;          
        case 3: //PNG
            imagepng($virtual_image, $filepath, 6);
            imagedestroy($virtual_image);
            break;          
        case 6: //WBMP
            imagewbmp($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        default:
            imagedestroy($virtual_image);
            break;          
    }
Kevin
  • 2,684
  • 6
  • 35
  • 64