1. 程式人生 > >php生成多尺寸縮圖記錄

php生成多尺寸縮圖記錄

 

在進行縮圖生成的時候,圖片的大小總是會同時儲存好幾個尺寸的,那該怎麼處理呢?

$filename = 'meinv.jpg';



list($src_w,$src_h,$imageTypeNum) = getimagesize($filename);



//獲取圖片mime型別 image_type_to_mime_type(int imagetype); image/jpeg

$imageTypeMime = image_type_to_mime_type($imageTypeNum);



//拼湊出 imagecreatefromjpeg()

$createFun = str_replace('/','createfrom',$imageTypeMime);



//拼湊出imagejpeg()

$outFun = str_replace('/',null,$imageTypeMime);



//建立圖片源

$src_img = $createFun($filename);



//建立縮圖片

$dst_50_image = imagecreatetruecolor(50,50);

$dst_220_image = imagecreatetruecolor(220,220);

$dst_350_image = imagecreatetruecolor(350,350);

$dst_800_image = imagecreatetruecolor(800,800);



//生成縮圖

imagecopyresampled($dst_50_image,$src_img,0, 0, 0, 0, 50,50,$src_w,$src_h);

imagecopyresampled($dst_220_image,$src_img,0, 0, 0, 0, 220,220,$src_w,$src_h);

imagecopyresampled($dst_350_image,$src_img,0, 0, 0, 0, 350,350,$src_w,$src_h);

imagecopyresampled($dst_800_image,$src_img,0, 0, 0, 0, 800,800,$src_w,$src_h);



//輸出縮圖

$outFun($dst_50_image,'./images_50/'.'50_'.$filename);

$outFun($dst_220_image,'./images_220/'.'220_'.$filename);

$outFun($dst_350_image,'./images_350/'.'350_'.$filename);

$outFun($dst_800_image,'./images_800/'.'800_'.$filename);



//銷燬圖片

imagedestroy($src_img);

imagedestroy($dst_50_image);

imagedestroy($dst_220_image);

imagedestroy($dst_350_image);

imagedestroy($dst_800_image);

 

 

效果如下:


 

上述的code的核心,就是通過圖片mime型別拼湊出 imagecreatefromjpeg() 、imagecreatefrompng()、imagejpeg() 、imagepng() 等函式呼叫… ~~!