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.
{
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;
}
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!
I will implement it soon, thank you.
I have bug which i would describe like this: when i use source image with bigger width than height (lets say 3:1 ratio), the function does not work at all. Any suggestions to fix it?
I agree with this comment, this would be a great add-in.
-david
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
Thanks Joanne – I appreciate your suggestions.
1) I will update this function with that block in the near future. : ) Great find! 2) From first glance your code does same thing as mine, only seems to be written a little better. Maybe there are some bugs I can’t notice, however – I was wondering what do you mean by “INSIDE the box” – what exactly was the problem? 3) That’s very nice. I had this in mind too, for the sake of overall convenience.
Appreciate comment. : )
(2) Here’s an example of what I meant by “inside the box:” Start with an original image 2400 pixels wide x 1600 pixels high. Call the function and specify width=800, height=600, proportional. With your algorithm, the image is resized to 900 x 600 pixels and it does not fit inside a 800 x 600 pixel box. With my algorithm the image is resized to 800 x 533 pixels.
I see exactly what you mean. This is a bug, and it only occurs in a few cases. Thank you for pointing it out! I will update the original code.
This is great – it works a treat for me. Thanks very much.
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.
Thank you for comment! I’d say, cropping is a separate issue. You never know what exactly you’re cropping out, – it might be that the whole point of the image is near its border. Resizing doesn’t actually alter the image, whereas cropping does, that’s why I decided to stay away from it, since most people require their images intact, but resized to specific dimensions.
Hi,
I actually came up with a few changes this afternoon to do with the cropping after finding a cropping function on the web. FYI, first I added a new argument -
function smart_resize_image($file, $width = 0, $height = 0, $proportional = false, $crop = false, $output = ‘file’, $delete_original = true, $use_linux_commands = false) {
Then added the following after the closing brace for if ($proportional) {…}
/* Start of new Code */ $int_width = 0; $int_height = 0;
$adjusted_height = $final_height; $adjusted_width = $final_width;
if ($crop) { $wm = $width_old/$width; $hm = $height_old/$height; $h_height = $height/2; $w_height = $width/2;
if ($width_old > $height_old) { $adjusted_width = $width_old / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width – $w_height; } else if(($width_old < $height_old) || ($width_old == $height_old)) { $adjusted_height = $height_old / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height – $h_height; } }
/* End new code */
Finally I changed the imagecopyresampled() function to -
imagecopyresampled($image_resized, $image, -$int_width, -$int_height, 0, 0, $adjusted_width, $adjusted_height, $width_old, $height_old);
How it works -> say you have an image 500 x 500 and want to crop & resize to 250 x 200, rather than crop from the top left and losing the bottom 100px of the image, it crops from 50px from the top instead, i.e. it centres the cropping area. It works both ways so if you want to crop to 200 x 250 instead, the cropping area is 50px in from the LHS.
I was going to get around to changing the variable names so that the final_height and final_width are used in the imagecopyresampled() function but didn’t get around to it yet. Also, I was thinking of setting the arguments so it would be either proportional resize, crop or none instead.
One other change I did do but not listed above was add a quality argument ($quality = ‘75′) and changed the imagejpeg() function call to imagejpeg($image_resized, $output, $quality);
Hope it helps.
Looks great! I will add some of these functionalities into the function soon. Also it seems that with this amount of parameters it would be reasonable to switch the function to accepting an associative array. It would be much easier if you could say something like array( ‘crop’ => false, ‘proportional’ => true, ‘output’ => ‘file’), and then understand what all those parameters mean when you come across this function in your code later.
I noticed a slight problem with the cropping on images that had a different width : height ratio to the final image. For example if I wanted to crop to 180 x 135 but my source image was 300 x 245, even though the width is greater than the height, I would not want to crop this so that I lost some of the width as it would end up with black borders on the side. Instead I would need to lose some of the height. So by knowing that the ratio we want is 1.3333, our source image has a ratio of 1.224 and as it is less, we know to crop using height instead. Looking at this another way, say our source image was 520 x 340, this has a ratio of 1.529 and so is greater than the target ratio we need to lose the width.
So my next changes are -
if ($crop) { $wm = $width_old/$width; $hm = $height_old/$height; $h_height = $height/2; $w_height = $width/2;
}
Now I’m hoping that this works better!
Alright, thanks! Glad you caught it. : )
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???
It depends on what kind of settings you choose when saving for web in photoshop. As for this function if it doesn’t work for you – try the older version over at php.net. I would appreciate if you let me know how was your experience. It might be that updates have screwed up something.
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!
Great thanks to thou, author. This transparency saving solution was helpful. Write more! :)
[...] Smart Image Resizing while preserving Transparency with PHP and the GD Library Sweet, I’ll definitely have to get back to this one! [...]
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:
$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.
I figured it out. I should have been using copy() and not move_uploaded_file.
Thanks for the great function!!!
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>
You don’t have to copy the file yourself. You can use $output parameter — it accepts any path where you want result image to appear. If you make $output to be “/path/to/directory” – it will copy file there for you. Just make sure delete_original = false for the first 2 conversions – and make delete_original = true for the last conversion, when you don’t need to keep original file anymore.
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
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
I’m seeing the same thing… 8bit png’s do not work
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!
Sounds like you’re missing GD Library in your PHP installation on the server. : )
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.
FANTASTIC :)
indeed!! thank you very much programmer. you’re fantastic, and your script too. thanks a bunch >:D<
So…how would I use this function to output a resized image to the browser?
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?
Hi, obviously you managed transparency on your debian-box … any hints what you had to change ?
This is very nice function
very very useful for me
Thanks a lot.
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
Let me know what kind of problem occurs and I will be able to help. : )
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 !
Well, I would say that cropping goes a little beyond this function’s intended use. Instead, you should set the output to “return”, this way you’ll have the image object that you can manipulate with gd library any way you like.
How do I use this script to output an image to the browser? Is there any example code?
+1 is there any example code?
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.
Try my code from http://mediumexposure.com/techblog/smart-image-resizing-while-preserving-transparency-php-and-gd-library#comment-98 which works fine for me.
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
And by “bad” I mean, lower quality images… This script has been a huge help!
I was just wondering how you set the file you want, how you would call this function and display the image in a browser?
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.
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]);
We have implemented a modified version of your code to our core web service! Works great on BuildaSearch.com! Cheers!
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:
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
Thank you for the part of the transparant crap!! Thankful to you forever ;)
[...] 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 [...]
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():
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.
<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
Hey Tim, Thanks for additions. The best way to contribute this type of thing is to fork the project on GitHub (see message on top of the article). Then I might be able to pull changes if they make sense for original intention here. : )
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!
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);}//end function
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.
{
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);
}
Hey, you aren’t a pain – all the comments help. I’m just kinda not maintaining this function much lately, that’s why I put it up on github – so others could easily fork it.
[...] ネットで調べたらsmart_image_resize関数ってやつを見つけました。 [...]
thank you! :D this was helpful! :D i just used it for something I’m writing for a friend :D
thanks again! :)
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']);
here’s a crop n scale function i think works really nicely could easily integrate into here
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;
}
?>
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.
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. :)
This function only resizes. To grab images from database you need to use php’s database APIs (like PDO), which differ depending on your db and php version. After you fetch images from db to filesystem you could perform any kind of processing such as this function.
If you have a “Donate to me for being awesome” fund, I’ll gladly chip in something when I get paid for this project. :)
brother, thanks soooo much! u are so kind
Thanks a lot for this excellent script!
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
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!!
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
I developed the same solution, but it was lacking the transparency solution…! Great work…!
This solution is a lot better than others I have found!
Thanks “Anonymous (not verified) on Wed, 02/18/2009 – 18:35″!
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
Hello. And Bye.
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