Sponsored by Sony Pictures
Watch a scene from 2012, in theaters November 13 view!
whowillsurvive2012.com - Get ready for the biggest event in history - the end of time. How will you survive? 2012- opening 11/13
25 Comments
- gharding, on 10/12/2007, -2/+15Wow. Way to copy the examples from www.php.net/imagecopyresampled .
- mmgm, on 10/12/2007, -0/+7You're wrong, frankly. I work for a big web2.0 website you almost surely know, and we use GD regularly. Of course, we cache the results, but we also cache everything else. It's not that bad, especially if you're running a big PHP-based operation and adding crontabbed Perl into the mix is just obscure and stupid.
- lcarsdeveloper, on 10/12/2007, -0/+4I use a thumbnail script that I modified:
http://www.olivo.net/software/imagethumb/
I added the thumbnail caching. What I do is get the last modified timestamp of the image, add the URL to it and get the md5 hash of the file. If there's a file in my cache folder with that md5 hash, then it means the original hasn't changed and I don't need to download the original and generate a new thumbnail. If the original can't be found, it just outputs a "broken image" picture I created. I use it on my forum to thumbnail remote images, it works great. If you post a 1024x768 image, it will take about 2 seconds to generate a thumbnail. From then on it takes around 0.25 seconds to display the thumbnailed image. Also, every time the thumbnail is used it touches the file so I know it's been retrieved recently. Then I run a CRON script to delete anything in the cache folder older than say 30 days. And I locked it down to certain sizes too, I list them in an array to prevent someone from filling the drive with thumbnails.
$sizes = array('50','80','100','120','150','200');
That way there can only be a maximum of 6 thumbnails for each image, rather than thousands if I left it opened. If I try to display a thumbnail at 300px, it will return my error image.
That's my method anyway. I used the thumbnail script in conjunction with some URL rewriting, so I have /thumbs/s/200/i/photos/main/2001.jpg
In that case, the maximum size is 200px and the original image sits in /images/photos/main/
(Remote urls use a different method)
If that helps anyone, feel free to let me know. I'm more than happy to help anyone out who wants to implement something like this. And if anyone has any ideas for improvements or possible problems I'd love to hear them. - prockcore, on 10/12/2007, -0/+3My favorite is:
convert src.jpg -thumbnail x200 -resize '200x<' -resize 50% -gravity center -crop 100x100+0+0 +repage src-thumb.jpg
It takes any size photo and makes a perfect square thumbnail from it (flickr style). - lcarsdeveloper, on 10/12/2007, -0/+3By the way, before anyone mods me down, I only posted that in case it helps someone. I think city17 should have posted a link to the code rather than the actual code.
And although I modified the script in the link, I didn't write it, and it's not my website. I took the basic script and added some new features to meet my requirements on a high-traffic site. - JeremyBanks, on 10/12/2007, -2/+5Yayus. Marked lame.
- dvws, on 10/12/2007, -0/+2I prefer PHP + ImageMagick as you can do a lot more with ImageMagick, and the processed images tend to look better. I can't really say if it is faster than GD, but most of the time you can run all of the operations in 1 shot as opposed to having to make a function call for every step.
Also, this article sucks, there are better GD function examples in the comments section of the manual. - jsd8cc, on 10/12/2007, -2/+3Can I dynamically generate my friend's head on the body of a porn star?
- misterdiggler, on 10/12/2007, -1/+2You jest but it could be done easily. Use face detect (say with OpenCV api) and slap it on. Not a bad plan...
- etx313, on 10/12/2007, -2/+3@Navin, you can try to load the php-gd lib into php as a plugin from the php.ini file but if your using shared hosting I doubt you will have access to it.
- etx313, on 10/12/2007, -2/+3Welcome to the 90's.
- putamare, on 10/12/2007, -0/+1A more thorough (and ad-free) tutorial is linked to from here: http://digg.com/programming/Introduction_to_PHP_Image_Functions
- NavinRJohnson, on 10/12/2007, -0/+1Thanks I will. but I am installing from putty. can you also be of some assistance in this area as well?
btw A good dark beer dose sound fn-Fabulous right about now. - negtive, on 10/12/2007, -0/+1Lots of great information in that link if you're willing to wade through it all. Better too much info than not enough tho!
- Curufir, on 10/12/2007, -0/+1Grab the rpm for php-gd that matches the version of php currently installed. Install rpm, make sure gd extension is enabled in php.ini, get beer and celebrate.
- NavinRJohnson, on 10/12/2007, -1/+1I am using versions of php mySql on an apache godaddy web server that is running on a fedora core 4 distro, unfortunately the version did not include "GD" I have already over 100 clients on that server so the risk of upgrading the php is a bit to risky for my knowledge.
Is there an easy way, or any way for that matter of manualy installing the gd onto this server. Here are my specs:
Server OS: Linux 2.6.16-1.2115_FC4_HPTRAID
HTTP Server: Apache/1.3.36 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.2 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.27 OpenSSL/0.9.7f
mySQL: 4.1.21
PHP Version: 4.4.2 (Zend: 1.3.0)
Can anyone help me. - marioluigi123, on 10/12/2007, -0/+0Dugg and bookmarked solely for the nifty tools. Thanks!
- city17, on 10/12/2007, -0/+0I posted the code cause there is no code, i simply posted it on php.net a few years ago..
- rantenki, on 10/12/2007, -0/+0Note to editors of phpit.net;
You should really vet the content written by your contributors, especially when it is running on a server, and ESPECIALLY if it could get digg'd. For the curious, go to the demo page here:
http://www.phpit.net/demo/introduction%20php%20image%20functions/2/resize.php
enter 500000 percent, then middle click to submit several instances of your image (use a small image, 128x128 or so). The server goes POOF! Limitations on image size are always a good idea on this kind of script, so from a DOS attack perspective, as well as from the coding difficulty, welcome to the nineties indeed! - dvws, on 10/12/2007, -2/+1oops
- bignickolson, on 10/12/2007, -2/+0flattery, maybe....
- affiliate, on 10/12/2007, -3/+1copycat = flatery?
- city17, on 10/12/2007, -8/+4I wrote this function a while ago, and i still find it the best for doing thumbnails..
function forceConstraints($srcFile, $srcType, $dstType, $dstWidth, $dstHeight, $dstPath)
{
if ($srcType == "image/jpeg")
$handle = @imagecreatefromjpeg($srcFile);
else if ($srcType == "image/png")
$handle = @imagecreatefrompng($srcFile);
else if ($srcType == "image/gif")
$handle = @imagecreatefromgif($srcFile);
else
return false;
if (!$handle)
return false;
$srcWidth = @imagesx($handle);
$srcHeight = @imagesy($handle);
if ($srcWidth >= $dstWidth && $srcHeight >= $dstHeight)
{
$newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);
if (!$newHandle)
return false;
if($srcHeight < $srcWidth)
{
$ratio = (double)($srcHeight / $dstHeight);
$cpyWidth = round($dstWidth * $ratio);
if ($cpyWidth > $srcWidth)
{
$ratio = (double)($srcWidth / $dstWidth);
$cpyWidth = $srcWidth;
$cpyHeight = round($dstHeight * $ratio);
$xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2);
} else {
$cpyHeight = $srcHeight;
$xOffset = round(($srcWidth - $cpyWidth) / 2);
$yOffset = 0;
}
} else {
$ratio = (double)($srcWidth / $dstWidth);
$cpyHeight = round($dstHeight * $ratio);
if ($cpyHeight > $srcHeight)
{
$ratio = (double)($srcHeight / $dstHeight);
$cpyHeight = $srcHeight;
$cpyWidth = round($dstWidth * $ratio);
$xOffset = round(($srcWidth - $cpyWidth) / 2);
$yOffset = 0;
} else {
$cpyWidth = $srcWidth;
$xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2);
}
}
if (!@imagecopyresampled($newHandle, $handle, 0, 0, $xOffset, $yOffset, $dstWidth, $dstHeight, $cpyWidth, $cpyHeight))
return false;
@imagedestroy($handle);
if ($dstType == "png")
@imagepng($newHandle, $dstPath.".png");
else if ($dstType == "jpg")
@imagejpeg($newHandle, $dstPath.".jpg", 90);
else if ($dstType == "gif")
@imagegif($newHandle, $dstPath.".gif");
else
return false;
@imagedestroy($newHandle);
return true;
} else {
return "Sorry, that image is too small. The image must be at least ".$dstWidth."x".$dstHeight." pixels in size.";
}
} - zeeneo, on 10/12/2007, -9/+3using php and gd for a busy site will kill the server - you seriously wont last a minute.
use a perl script with ImageMagick on a cron job if you can. PHP in my opinion is a web templating system - not an image processor.
ImageMagick + Perl is a lot more powerful than PHP + GD! - mv36, on 10/12/2007, -9/+1We took down his sight for copying an article ;)
Legitimate DDOS :D
Oh well mirrored at,
http://www.duggmirror.com/programming/More_Image_manipulation_with_PHP_the_GD_library/
What is Digg?