Smart Image Resizing while Preserving Transparency With PHP and GD Library

Update: this function is now available at github.com/maxim/smart_resize_image.
Fork away!

Should be easy to find, right? All you want is a function that resizes an image to constraints (doesn’t care if it should scale up or down), with possibility to select if you want to keep it proportional, and possibility to use either width or height as the constraint. Also, you want it to preserve transparency damn it! Surprisingly, I was unable to find a good function that does all that, so I decided to attempt writing it. This should do it.

Features/Usage:

  • If you pass width as 0 (zero) — this function will disregard width, and use height as constraint. Same vice versa.
  • If you set “proportional” to false – the function will simply stretch (or shrink) the image to its full constraints.
  • If one of the dimensions is set to zero, and proportional set to “false” – then the image will be forced to stretch or shrink the other dimension, and disregard the zeroed dimension (leave it the same).
  • If proportional is set to true – the image will resize to constraints proportionally, once again, with possibility to have either width or height set to zero.
  • The function can use either linux “rm” command, or php @unlink. Most probably you don’t need to ever use that flag, but on some setups – @unlink won’t work due to user access restrictions.
  • The function will simply replace the file that you give it, with the resized file.
  • The function supports gif, png, and jpeg, and preserves the transparency of gif and png images.
  • Tested on GD version 2.0.28 only.

Essentially, everything happens just as logically expected. Please, if you see anything wrong, and you know ways to enhance, or optimize it, let me know.

Update: Thank you Joanne for finding a bug with proportional resize, and providing a great, more concise and clear solution to proportional resize. (see comments)

Update: The image transparency code was updated to a more thorough solution. See comments for info.

Update: There is a new parameter “output” which can be set to either

  • “file” – overwrite the given file (default)
  • “browser” – output image through http – with correct mime type
  • “return” – return GD Library Image object
  • or any filename of choice – save changed version to output path

Update: There is another new parameter “delete_original”. Speaks for itself.

Please, excuse a few code misalignments. Bug with coloring filter.

function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
  {
    if ( $height <= 0 && $width <= 0 ) {
      return false;
    }

    $info = getimagesize($file);
    $image = '';

    $final_width = 0;
    $final_height = 0;
    list($width_old, $height_old) = $info;

    if ($proportional) {
      if ($width == 0) $factor = $height/$height_old;
      elseif ($height == 0) $factor = $width/$width_old;
      else $factor = min ( $width / $width_old, $height / $height_old);  

      $final_width = round ($width_old * $factor);
      $final_height = round ($height_old * $factor);

    }
    else {
      $final_width = ( $width <= 0 ) ? $width_old : $width;
      $final_height = ( $height <= 0 ) ? $height_old : $height;
    }

    switch ( $info[2] ) {
      case IMAGETYPE_GIF:
        $image = imagecreatefromgif($file);
      break;
      case IMAGETYPE_JPEG:
        $image = imagecreatefromjpeg($file);
      break;
      case IMAGETYPE_PNG:
        $image = imagecreatefrompng($file);
      break;
      default:
        return false;
    }
   
    $image_resized = imagecreatetruecolor( $final_width, $final_height );
       
    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
      $trnprt_indx = imagecolortransparent($image);
   
      // If we have a specific transparent color
      if ($trnprt_indx >= 0) {
   
        // Get the original image's transparent color's RGB values
        $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
   
        // Allocate the same color in the new image resource
        $trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
   
        // Completely fill the background of the new image with allocated color.
        imagefill($image_resized, 0, 0, $trnprt_indx);
   
        // Set the background color for new image to transparent
        imagecolortransparent($image_resized, $trnprt_indx);
   
     
      }
      // Always make a transparent background color for PNGs that don't have one allocated already
      elseif ($info[2] == IMAGETYPE_PNG) {
   
        // Turn off transparency blending (temporarily)
        imagealphablending($image_resized, false);
   
        // Create a new transparent color for image
        $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
   
        // Completely fill the background of the new image with allocated color.
        imagefill($image_resized, 0, 0, $color);
   
        // Restore transparency blending
        imagesavealpha($image_resized, true);
      }
    }

    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
 
    if ( $delete_original ) {
      if ( $use_linux_commands )
        exec('rm '.$file);
      else
        @unlink($file);
    }
   
    switch ( strtolower($output) ) {
      case 'browser':
        $mime = image_type_to_mime_type($info[2]);
        header("Content-type: $mime");
        $output = NULL;
      break;
      case 'file':
        $output = $file;
      break;
      case 'return':
        return $image_resized;
      break;
      default:
      break;
    }

    switch ( $info[2] ) {
      case IMAGETYPE_GIF:
        imagegif($image_resized, $output);
      break;
      case IMAGETYPE_JPEG:
        imagejpeg($image_resized, $output);
      break;
      case IMAGETYPE_PNG:
        imagepng($image_resized, $output);
      break;
      default:
        return false;
    }

    return true;
  }

93 responses to “Smart Image Resizing while Preserving Transparency With PHP and GD Library”

  1. VilksPilks

    Very nice! I thougth it was impossible to create high quality thumbnails using php. This article proved me wrong. I would recomend to add quality paremeter to imagejpeg, like imagejpeg($image_resized, $file, 95); or such, default 75 leaves to many artifacts. Anyway, a very usefull article and a suburb code!

    1. David

      I agree with this comment, this would be a great add-in.

      -david

  2. Joanne

    Thanks for posting this script on the PHP manual site. I really like having a script that handles any standard image type in one place.

    You asked for feedback and suggestions. I made a few changes for my implementation, which you may or may not think are improvements:

    (1) Transparency for GIFs was not working for me…all backgrounds were black. I changed the transparency section of your script to the one posted by Rob on 9-Oct-2007 on the imagecolortransparent page of the PHP site, and it works well: http://www.php.net/manual/en/function.imagecolortransparent.php

    (2) When both width and height are specified for proportional resizing, I thought the new image should fit INSIDE the box specified by width and height. So I changed your proportional resize algorithm to:

    < ?php if ($width == 0) $factor = $height/$height_old; elseif ($height == 0) $factor = $width/$width_old; else $factor = min($width/$width_old,$height/$height_old); $final_width = round ($width_old * $factor); $final_height = round ($height_old * $factor); ?>

    (3) I wanted the script to be able to output either to a file or to the browser. At the place where you delete the old file, I changed it to:

    < ?php if ($output == 'file') unlink($file); else { // output to browser $mime = image_type_to_mime_type($info[2]); header("Content-type: $mime"); $file = NULL; } ?>

    Thanks again…I’ve had fun playing with this.

    Joanne

  3. Dave Gardner

    This is great – it works a treat for me. Thanks very much.

  4. Dave

    Excellent function. Really useful.

    I was wondering, however, if you planned to add a crop function in as well? For example, if you allowed people to upload images to your site but you wanted to keep them within a set size (say 300 x 200) and someone uploads an image at 600 x 600, rather than resizing the image to fit the 300 x 200 area, why not first crop the image to 600 x 400, then resize? This would avoid distorting the image.

    Just a thought.

    Thanks anyway.

  5. Felix

    I am having difficulty with the transparency. Sometimes my images have a completely black background and no transparency. Sometimes they have a dithered background with some pixels transparent and others not.

    I’m using GIFs saved from Photoshop either with the Save to Web function or just the Save As.

    Any ideas???

  6. Justin

    This is a great bit of code. Exactly what I needed. There doesn’t seem to be much reference on how to keep the transparency of png images and l was having real problems with it, your code helped me sort it. Thanks!

  7. Demon

    Great thanks to thou, author. This transparency saving solution was helpful. Write more! :)

  8. All in a day's work...

    [...] Smart Image Resizing while preserving Transparency with PHP and the GD Library Sweet, I’ll definitely have to get back to this one! [...]

  9. Mike

    Help I can’t figure out how to use this excellent function to make multiple resizes and upload them to separate directories (for larger, medium-sized, and thumbnail images). Here is my code:

    $full = $_FILES['upload']['tmp_name'][$number];      smart_resize_image( $full, $width = 400, $height = 0,
    $proportional = true, $output = 'file',
    $delete_original = true, $use_linux_commands = true );<br />
    smart_resize_image( $full, $width = 240, $height = 0, $proportional = true, $output = '$display', $delete_original = true, $use_linux_commands = true );<br />
    smart_resize_image( $display, $width = 120, $height = 0, $proportional = true, $output = '$thumb', $delete_original = true, $use_linux_commands = true );<br />
    $success_full = move_uploaded_file($full, FULL_DIR.$file_name);
    $success_display = move_uploaded_file($display, DISPLAY_DIR.$file_name);
    $success_thumbs = move_uploaded_file($thumb, THUMB_DIR.$file_name);

    Sorry I know I’m a newbie at this kind of thing. I’ve tried DELETE_ORIGINAL = false, I’ve tried renaming the image files. No relief from my frustration.

    1. Mike

      I figured it out. I should have been using copy() and not move_uploaded_file.

      Thanks for the great function!!!

            /* USE SMART_RESIZE FUNCTION TO MAKE 3 COPIES AND UPLOAD TO MEDIA DIR */
            smart_resize_image( $_FILES['upload']['tmp_name'][$number], $width = 240, $height = 0, $proportional = true, $output = 'file', $delete_original = false, $use_linux_commands = false );
            $success_display = copy($_FILES['upload']['tmp_name'][$number], DISPLAY_DIR.$file_name);
            smart_resize_image( $_FILES['upload']['tmp_name'][$number], $width = 400, $height = 0, $proportional = true, $output = 'file', $delete_original = false, $use_linux_commands = false );
            $success_full = copy($_FILES['upload']['tmp_name'][$number], FULL_DIR.$file_name);
            smart_resize_image( $_FILES['upload']['tmp_name'][$number], $width = 120, $height = 0, $proportional = true, $output = 'file', $delete_original = true, $use_linux_commands = false );
            $success_thumbs = move_uploaded_file($_FILES['upload']['tmp_name'][$number], THUMB_DIR.$file_name);</p>

      <p>

  10. thor918

    hi. thanks a lot for this code! It’s the best commented one and the best working one I have found!

    when using proportional resizing, I wanted to have some changes. if you set maxwidt=100px,maxheight=100px, the resultimage can be for example 100×75. In my case I wanted that image to fixed to maxwidt and maxheight, and still have the picture propotional resized. I have made it create a transparent frame that is fixed to maxwidth an maxheight, the propotional picture is then centered to that one.

    look on the end of this page:(you should be able to see the code there without logging in) http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23036568.html

  11. Dane

    Thank you for writing this. It is exactly what I have been looking for all day, but there is one problem. GIF’s and 8 bit PNG’s do not show transparency correctly. GIF’s just come out with white wherever it is supposed to be transparent and then 8 bit PNG’s end up distorted-looking with random pixels transparent. 24-bit PNG’s look great though.

    Thanks

    1. Thor

      I’m seeing the same thing… 8bit png’s do not work

  12. ahhoi

    I used this great script for a small Web project and it works fine on my local computer in an (OSX 10.4) PHP 4 environment. Recently I upgraded to OSX 10.5 that works with PHP5 and now I get this fatal error: ‘call to undefined funtion imagecreatefromjpeg()’ it’s probably just a tiny thing but anyone here with the same problem and a suggestion how to fix it? thx a lot!

  13. Ryan

    Thanks, this is just what i was looking for, really made life easier for me!

    Cheers! R

    P.S Please do something about your captcha… its one of the most irritating that i have ever come accross.

  14. Chris

    FANTASTIC :)

    1. Catalin

      indeed!! thank you very much programmer. you’re fantastic, and your script too. thanks a bunch >:D<

  15. Ted

    So…how would I use this function to output a resized image to the browser?

  16. Joe

    Hi,

    thanks for the great solution to preserving transparency. On Debian Etch I found that the transparency is not coming out correctly, and instead a grey background shows up (try for example this image: http://www.nba.com/media/bos_50px_080328.gif

    Any others experiencing the same thing?

    1. alex

      Hi, obviously you managed transparency on your debian-box … any hints what you had to change ?

  17. Stranger

    This is very nice function

    very very useful for me

    Thanks a lot.

  18. jeff

    Hi Max;

    you have nice script . i’m new to php and im trying to get a script that risizes tranparent gif and display the image in the screen. I spentlot of time playingwith your wornderfull script , but could not worked it. any help and scipt example would be appreciated

    thanks jeff

  19. geo

    Is there a way to make the crop area to stand more ‘up’ than the current position (vertically) considerring that i have pictures which containt valued information mainly on the uppper part of the image. Last, but not least is to say that the function is great! Works excellent with no black lines, no matter what the source and destination is !

  20. Ned

    How do I use this script to output an image to the browser? Is there any example code?

    1. Stranger

      +1 is there any example code?

  21. brad

    I couldn’t get transparency working in GIFs unless I used imagecreate() instead of imagecreatetruecolor(). I guess GIF is palette-based, so the color indexes get messed up. php.net actually says “This function will not work with GIF file formats,” although I think they’re lying since it just doesn’t play nice with transparency.

    1. carbonize
  22. David Arnold

    To change the quality for JPEG simply modify the following lines.

    function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = ‘file’, $delete_original = true, $use_linux_commands = false ) function smart_resize_image( $file, $quality, $width = 0, $height = 0, $proportional = false, $output = ‘file’, $delete_original = true, $use_linux_commands = false )

    And then:

    imagejpeg($image_resized, $output); imagejpeg($image_resized, $output, $quality);

    Just thought I would share this in case anyone else was running into bad quality images (default 75) produced by this script.

    Thanks, David

    1. David Arnold

      And by “bad” I mean, lower quality images… This script has been a huge help!

  23. Scott

    I was just wondering how you set the file you want, how you would call this function and display the image in a browser?

  24. Strahil

    Don’t know why it’s missing, but here’s a simple solution for rotating the image:

    if ($rotate > 0) { $image_resized = imagerotate($image_resized, $rotate, 0); }

    You just add $rotate = 0 in the arguments.

  25. Carbonize

    Why not just make the new image transparent before using imagecopyresampled?

    $im = ImageCreateFromPNG($source); $new_im = imagecreatetruecolor($new_size[0],$new_size[1]); imagecolortransparent($new_im, imagecolorallocate($new_im, 0, 0, 0)); imagecopyresampled($new_im,$im,0,0,0,0,$new_size[0],$new_size[1],$size[0],$size[1]);

  26. IcedTea

    We have implemented a modified version of your code to our core web service! Works great on BuildaSearch.com! Cheers!

  27. Tom

    Thanks for this script, it is great but I wanted to ask if anyone has run into problems when processing large images?

    I have a image that is 2.9MB large and the smart_resize_image function gives a PHP error because more memory is needed than the limit I have set in my php.ini file (32M). Does anyone know why so much memory is needed (i.e. over 32MB for a 2.9MB file) and if there is a workaround other than increasing memory_limit in php.ini?

    Specific details:

    • Problematic line:
      • $image = imagecreatefromjpeg($file);
    • php.ini:
      • memory_limit = 32M
    • Error Msg:
      • Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 13056 bytes) in /blah/library/imageProcessing.php on line 41).
  28. Tom

    To answer my own question about running out of memory…

    Put this in the PHP script that calls smart_resize_image(…): < ?php ini_set('memory_limit', '50M'); ?>

    For more details about memory usage of the imagecreatefromjpeg function, see http://uk2.php.net/imagecreatefromjpeg

  29. Gersom

    Thank you for the part of the transparant crap!! Thankful to you forever ;)

  30. smart_resize_image now hosted on GitHub | Medium eXposure

    [...] while ago I wrote a php function for image resizing with preservation of transparency. It seems to have been quite popular, but until now there was no good way to fork the code, and/or [...]

  31. Tim

    Hey, great function.. I had four or five of these types of scripts I was using independently for different purposes and your function seems to bring all the elements together.

    Wanted to add something to it in return. How about a watermark option?

    Just added the $apply_watermark argument to smart_resize():

    function smart_resize_image( $file, $width = 325, $height = 0, $proportional = true, $output = 'browser', $delete_original = false, $use_linux_commands = false, $apply_watermark = true )

    Then added this if/else construct to replace the imagecopyresampled() function with code that combines the re-sized image with a watermark gif, (could be a png I suppose), if $apply_watermark is true.. Otherwise it functions like normal.

    if ($apply_watermark) {</p>

    <pre><code>$outputWidth = floor($rawWidth * $imageScaleTo);
    $outputHeight = floor($rawHeight * $imageScaleTo);

    $outputImage = Imagecreatetruecolor($final_width, $final_height);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

    // Turn on alpha blending
    imagealphablending($outputImage, true);

    // Create overlay image - Could be a png?
    $watermark = imagecreatefromgif("../images/watermark.gif");

    // Get the offset centering for the overlay
    $offsetWidth = floor(($final_width - imagesx($watermark))/2);
    $offsetHeight = floor(($final_height - imagesy($watermark))/2);

    // Overlay watermark
    imagecopy($image_resized, $watermark, $offsetWidth, $offsetHeight, 0, 0, imagesx($watermark), imagesy($watermark));


    } else {

        imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
    }

    Hope this is useful to somebody.

    Question though.. Is it possible to use imagecopyresampled() instead of imagecopy()? I just couldn't get it to work somehow.. I Dunno, it's like quarter to four in the morning and I'm getting bleary eyed.. :-)

    Anyway..

    Once again, thanks for posting this excellent function!

    -TIM

  32. Anonymous

    Hi,

    I thought that this script was working well, but I’m recieving an error on some transparent gifs:

    Warning: imagecolorsforindex() [function.imagecolorsforindex]: Color index 63 out of range in /home/amarien/public_html/rocket/uploadprocessor.php on line 396

    The image still uploads and looks fine. Why the error?

    Any ideas about imagecolorsforindex()?

    Thanks!

  33. Anonymous

    I thought I’d post my code from the comment below… any input would be appreciated.

    //function to save the image to the server
    function saveAdjustedImage($width, $height, $new_width, $new_height, $mime, $file)
    {
        //create the image
        $adjusted_image = imagecreatetruecolor($new_width, $new_height);

    
    
    //resize jpg
    if(($mime == "image/jpg") || ($mime == "image/jpeg"))
    {  
        //get the image from the server
        $source = imagecreatefromjpeg($file);

        //resize the new image
        imagecopyresized($adjusted_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        //save the image
        imagejpeg($adjusted_image, $file);
    }

    //resize the gif
    elseif(($mime =="image/gif") || ($mime == "image/png"))
    {
        if($mime =="image/gif")
        {
            //get the image from the server
            $source = imagecreatefromgif($file);
        }
        else
        {
            //get the image from the server
            $source = imagecreatefrompng($file);
        }

        //chack for transparency
        $trnprt_indx = imagecolortransparent($source);

        //if the image is transparent, keep it's transparency      
        if ($trnprt_indx &gt;= 0)
        {
            // Get the original image's transparent color's RGB values
            $trnprt_color = imagecolorsforindex($source, $trnprt_indx);

            // Allocate the same color in the new image resource
            $trnprt_indx = imagecolorallocate($adjusted_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

            // Completely fill the background of the new image with allocated color.
            imagefill($adjusted_image, 0, 0, $trnprt_indx);

            // Set the background color for new image to transparent
            imagecolortransparent($adjusted_image, $trnprt_indx);
        }

        // Always make a transparent background color for PNGs that don't have one allocated already
        if($mime == "image/png")
        {
            // Turn off transparency blending (temporarily)
            imagealphablending($adjusted_image, false);

            // Create a new transparent color for image
            $color = imagecolorallocatealpha($adjusted_image, 0, 0, 0, 127);

            // Completely fill the background of the new image with allocated color.
            imagefill($adjusted_image, 0, 0, $color);

            // Restore transparency blending
            imagesavealpha($adjusted_image, true);
        }

        //adjust image and save to the server
        imagecopyresampled($adjusted_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        if($mime =="image/gif")
        {
            imagegif($adjusted_image, $file);
        }
        else
        {
            imagepng($adjusted_image, $file);
        }

    }//end else if
    else
    {
        echo "There was an problem with your file. Please try again.";
        exit();
    }

    }//end function

    1. Anonymous

      Hi again,

      Sorry to be a pain in the ass. I found another script and tweaked it. The solution below is working for me. I thought I’d post it in case anyone else has trouble with the current code in the blog. I’m still unsure why the original code didn’t work, but here is my current solution.

      Thanks for a great resource.

          elseif(($mime =="image/gif") || ($mime == "image/png"))
          {
              if($mime =="image/gif")
              {
                  //get the image from the server
                  $source = imagecreatefromgif($file);
              }
              else
              {
                  //get the image from the server
                  $source = imagecreatefrompng($file);
              }</p>

      <pre><code>    imagealphablending($adjusted_image, false);
                  imagesavealpha($adjusted_image,true);
                  $transparent = imagecolorallocatealpha($adjusted_image, 255, 255, 255, 127);
                  imagefilledrectangle($adjusted_image, 0, 0, $new_width, $new_height, $transparent);

          imagecopyresampled($adjusted_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

          if($mime =="image/gif")
          {
              imagegif($adjusted_image, $file);
          }
          else
          {
              imagepng($adjusted_image, $file);
          }

  34. GDでリサイズ時にGIFやPNGの透過を維持する方法

    [...] ネットで調べたらsmart_image_resize関数ってやつを見つけました。 [...]

  35. Saliem

    thank you! :D this was helpful! :D i just used it for something I’m writing for a friend :D

    thanks again! :)

  36. Vicent González i Castells

    Thank you. This code is very usefull.

    I obtained better results in transparent quality for gifs with this modification. If this param isn’t added, transparent parts are dirty, with undesired pixels.

    Observe “$trnprt_color['blue']” added as param at the end.

    … // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue'], $trnprt_color['alpha']); …

    This alternative line also works $trnprt_indx = imagecolorallocatealpha($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

  37. Anonymous

    here’s a crop n scale function i think works really nicely could easily integrate into here

    <?php
    function crop_n_scale($src_img, $dimx, $dimy)
    {
        // functional scaling
        $srcH = imagesy($src_img);
        $srcW = imagesx($src_img);
        $ratio = $dimy/$srcH;
        $new_W = $srcW*$ratio;
       
        if ($new_W<$dimx)
            $ratio = $dimx/$srcW;
       
        //$img = scale_image($src_img, $srcW*$ratio, $srcH*$ratio);
        $img=imagecreatetruecolor($srcW*$ratio,$srcH*$ratio);
        imagecopyresampled($img,$src_img,0,0,0,0,$srcW*$ratio,$srcH*$ratio,$srcW,$srcH);

        // crop (if necessary)
        $img = crop_image($img, $dimx, $dimy);
        return $img;
    }
    ?>

  38. Anonymous

    Thanks for the post, the code is really useful. PHP GD functions are not very well documented in PHP, the sample code gives me a much better understanding as to how the GD functions work.

  39. Anonymous

    Hey there!

    First of all, amazing and helpful function. :) Thanks so much for putting it out there for the world to use.

    Second, I have a slight problem. A customer with a web 2.0ish site has had all of their user-uploaded content saved into an SQL DB. This includes images.

    As I am simply useless when it comes to these image mod in PHP stuff, do you have any suggestions as to how to move those images into the filesystem? I have the content type and the images themselves saved as a blob in the DB.

    I was hoping, perhaps naively, to use your smart_image_resize function; but I guess this is a no-go!

    Anyways, thanks so much for the function and even more if you can provide a tip for me. :)

  40. mc

    brother, thanks soooo much! u are so kind

  41. Stefan

    Thanks a lot for this excellent script!

  42. uMMM

    Thx guys alooooooooot for this finally something usefull about GD ;)

    But does anyone have any idea how to gain more quality on watermarks? What files to use?

    I have codes that are doing watermarks but result is so bad! Colors are really totaly disorted & fzzy.

    Anyone have any clue..?

    Thx & all the best guys :P

  43. Robert

    Thanks for the script, it works great. Forgive me for the newbie question, but I’m trying to pass the filename as a variable with a tn_prefix and my syntax must be off because the screen goes blank and I get no new image

    function smart_resize_image( $file, $width = 100, $height = 100, $proportional = false, $output = ‘testupload/tn_’.$imagename, $delete_original = false, $use_linux_commands = false )

    Any pointers would be great!

    Thanks again for preserving my png transparencies!!

  44. SachinKRaj

    Thank you for such a brilliant solution for handling transparent images. I knew and developed image re size methods but somewhere they were all failing with respect to PNG and GIF images.

    Thanks a lot! Sach

  45. Ahsan Abdul Jabbar

    I developed the same solution, but it was lacking the transparency solution…! Great work…!

  46. Matt

    This solution is a lot better than others I have found!

  47. Anonymous

    Thanks “Anonymous (not verified) on Wed, 02/18/2009 – 18:35″!

  48. Anonymous

    thanks 4 the lesson and .. you figured the stuff about transparency transfer ; that are not pointed out in the majority of php png manipulation methods.

    P.S. png transparency thumbnail alpha blend png resize resizing tool function

  49. XRumerTest

    Hello. And Bye.

  50. Sanjeev

    it really works great as i tried for png. But for gif image it dint gave the output as i expected. May be the original image 360×211 in dimension, while i was resizing to 300×300(with aspect ratio maintained) it gave kind of zigzag colored image

Leave a Reply