Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Image resize function

    Is there a PHP function to resize an image? I've googled it and found pre made scripts to do it but i wanna have a go at making one. All im after is the function name like "resizeimage()",so I can google it at find out about it. Thanks

  • #2
    New Coder
    Join Date
    Mar 2011
    Posts
    62
    Thanks
    3
    Thanked 2 Times in 2 Posts
    You can put html inside of php...

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Sorry, i wasnt being clear >.<. I mean like for when someone uploads an image, if they attach an image 10000x10000 it will be resized to 1000x1000. Thats the kinda resize i mean.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this function, it requires imagick to work:

    PHP Code:
        function ResizeImage($saveToDir$imagePath$imageName$max_x$max_y){
            if (
    file_exists($imagePath)) {

                
    $image  = new Imagick($imagePath);
                
    $height $image->getImageHeight();
                
    $width  $image->getImageWidth();
                if (
    $width $max_x)  $image->scaleImage($max_x,$max_y,true);
                if (
    $height $max_y$image->scaleImage($max_x,$max_y,true);
                
    $image->writeImage($saveToDir $imageName);
            }
        } 

  • Users who have thanked angst for this post:

    tomharto (04-21-2011)

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Thanks, ill give that a go when i get home . Currently stuck at work when it warm and sunny outside -.-

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    I'm sure this can be simplified, but if you don't have imagik, here's what I use:

    Code:
    list($imgWidth, $imgHeight, $imgType) = getimagesize($source);
    if (image_type_to_mime_type($imgType) == 'image/jpeg' || image_type_to_mime_type($imgType) == 'image/pjpeg') $tempImg = imagecreatefromjpeg($source);
    elseif (image_type_to_mime_type($imgType) == 'image/gif') $tempImg = imagecreatefromgif($source);
    elseif (image_type_to_mime_type($imgType) == 'image/png') $tempImg = imagecreatefrompng($source);
    
    $xRatio = $maxWidth / $imgWidth;
    $yRatio = $maxHeight / $imgHeight;
    
    if ($imgWidth <= $maxWidth && $imgHeight <= $maxHeight) {
    	$finalWidth = $imgWidth;
    	$finalHeight = $imgHeight;
    } elseif (($xRatio * $imgHeight) < $maxHeight) {
    	$finalWidth = $maxWidth;
    	$finalHeight = ceil($xRatio * $imgHeight);
    } else {
    	$finalWidth = ceil($yRatio * $imgWidth);
    	$finalHeight = $maxHeight;
    }
    
    $tempColor = imagecreatetruecolor($finalWidth, $finalHeight);
    imagecopyresampled($tempColor, $tempImg, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight);
    
    if ($extension == 'jpg' || $extension == 'jpeg') imagejpeg($tempColor, $destination, 100);
    elseif ($extension == 'gif') imagegif($tempColor, $destination);
    elseif ($extension == 'png') imagepng($tempColor, $destination);
    imagedestroy($tempImg);
    imagedestroy($tempColor);
    As you can see, this come from a use where you can change the max height/width (through a function).

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    here's a good function that uses GD

    PHP Code:
        function ResizeImage($saveToDir$imagePath$imageName$max_x$max_y){
           
    preg_match("'^(.*)\.(gif|jpe?g|png)$'i"$imageName$ext);
           switch (
    strtolower($ext[2])) {
               case 
    'jpg' $im  imagecreatefromjpeg ($imagePath);
                             break;
               case 
    'jpeg'$im  imagecreatefromjpeg ($imagePath);
                             break;
               case 
    'gif' $im  imagecreatefromgif  ($imagePath);
                             break;
               case 
    'png' $im  imagecreatefrompng  ($imagePath);
                             break;
               default    : 
    $stop true;
                             break;
           }

           if (!isset(
    $stop)) {

               
    $x imagesx($im);
               
    $y imagesy($im);

               if ((
    $max_x/$max_y) < ($x/$y)) {
                   
    $save imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x));
               } else {
                   
    $save imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y));
               }
               
    imagecopyresized($save$im0000imagesx($save), imagesy($save), $x$y);
               
               switch (
    strtolower($ext[2])) {
                   case 
    'jpg' imagejpeg($save"{$saveToDir}{$ext[1]}.".$ext[2]."",100);
                                 break;
                   case 
    'jpeg'imagejpeg($save"{$saveToDir}{$ext[1]}.".$ext[2]."",100);
                                 break;
                   case 
    'gif' imagegif($save"{$saveToDir}{$ext[1]}.".$ext[2]."");
                                 break;
                   case 
    'png' imagepng($save"{$saveToDir}{$ext[1]}.".$ext[2]);
                                 break;
               }
               
    imagedestroy($im);
               
    imagedestroy($save);
           }
        } 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •