|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Resize Image Function With PHPCategory : PHP ScriptDo you have problem with resize image on the fly with php? or why i must resize image with php? you need to resize your image, because if your user give you image with 1000x1000 and you only display it on 100x100 it will waste your bandwidth. So to solve your problem here is function to resize your image on the fly with php <? function resize_img($source,$dest,$MaxWidth,$MaxHeight,$Quality) { list($ImageWidth,$ImageHeight,$TypeCode)=getimagesize($source); $ImageType=($TypeCode==1?"gif": ($TypeCode==2?"jpeg": ($TypeCode==3?"png":FALSE))); $CreateFunction="imagecreatefrom".$ImageType; $OutputFunction="image".$ImageType; if ($ImageType) { $Ratio=($ImageHeight/$ImageWidth); $ImageSource=$CreateFunction($source); if ($ImageWidth > $MaxWidth || $ImageHeight > $MaxHeight) { if ($ImageWidth > $MaxWidth) { $ResizedWidth=$MaxWidth; $ResizedHeight=$ResizedWidth*$Ratio; } else { $ResizedWidth=$ImageWidth; $ResizedHeight=$ImageHeight; } if ($ResizedHeight > $MaxHeight) { $ResizedHeight=$MaxHeight; $ResizedWidth=$ResizedHeight/$Ratio; } $ResizedImage=imagecreatetruecolor($ResizedWidth,$ResizedHeight); imagecopyresampled($ResizedImage,$ImageSource,0,0,0,0,$ResizedWidth, $ResizedHeight,$ImageWidth,$ImageHeight); } else { $ResizedWidth=$ImageWidth; $ResizedHeight=$ImageHeight; $ResizedImage=$ImageSource; } $OutputFunction($ResizedImage,$dest,$Quality); return true; } else return false; } ?> Just copy and paste above function to your php file. here is some example how it works <? resize_img("/path/to/source/images.gif","/path/to/destination/image.gif",200,200,100); // above comment will resize your orginal image to 200 x 200 ?> Hope this function can help you Facebook
<< Back 0 Comment
| 1190 hits | Posted by Callrid at 2011-01-01 06:28:33
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Last Modified
08-November-2011, 23:21:04 pm callrid.com Created 2006-2012 by Anton Ongsono All rights reserved |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||