本文實(shí)例講述了PHP實(shí)現(xiàn)原比例生成縮略圖的方法。分享給大家供大家參考,具體如下:
<?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); // 創(chuàng)建目標(biāo)圖gd2} else { $dim = imagecreate($yy, $xx); // 創(chuàng)建目標(biāo)圖gd1}imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);header ("Content-type: image/jpeg");imagejpeg ($dim, false, 100);?>
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。