這篇文章主要介紹了php動態生成縮略圖并輸出顯示的方法,涉及php操作圖片的相關技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php動態生成縮略圖并輸出顯示的方法。分享給大家供大家參考。具體如下:
調用方法:
- <img src="thumbs.php?filename=photo.jpg&width=100&height=100">
此代碼可以為大圖片動態生成縮略圖顯示,圖片在內存中生成,不在硬盤生成真實文件
thumbs.php文件如下:
- <?php
- $filename= $_GET['filename'];
- $width = $_GET['width'];
- $height = $_GET['height'];
- $path="http://localhost/images/"; //finish in "/"
- // Content type
- header('Content-type: image/jpeg');
- // Get new dimensions
- list($width_orig, $height_orig) = getimagesize($path.$filename);
- if ($width && ($width_orig < $height_orig)) {
- $width = ($height / $height_orig) * $width_orig;
- } else {
- $height = ($width / $width_orig) * $height_orig;
- }
- // Resample
- $image_p = imagecreatetruecolor($width, $height);
- $image = imagecreatefromjpeg($path.$filename);
- imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
- // Output
- imagejpeg($image_p, null, 100);
- // Imagedestroy
- imagedestroy ($image_p);
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答