本文實例講述了PHP實現原比例生成縮略圖的方法。分享給大家供大家參考,具體如下:
<?php
$image = "jiequ.jpg"; // 原圖
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$x = imagesx($im);//獲取圖片的寬
$y = imagesy($im);//獲取圖片的高
// 縮略後的大小
$xx = 140;
$yy = 200;
if($x>$y){
//圖片寬大於高
$sx = abs(($y-$x)/2);
$sy = 0;
$thumbw = $y;
$thumbh = $y;
} else {
//圖片高大於等於寬
$sy = abs(($x-$y)/2.5);
$sx = 0;
$thumbw = $x;
$thumbh = $x;
}
if(function_exists("imagecreatetruecolor")) {
$dim = imagecreatetruecolor($yy, $xx); // 創建目標圖gd2
} else {
$dim = imagecreate($yy, $xx); // 創建目標圖gd1
}
imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);
header ("Content-type: image/jpeg");
imagejpeg ($dim, false, 100);
?>
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》及《php常用函數與技巧總結》
希望本文所述對大家PHP程序設計有所幫助。