PHP實現(xiàn)原比例生成縮略圖的方法
更新時間:2016年02月03日 11:15:12 作者:不是JS
這篇文章主要介紹了PHP實現(xiàn)原比例生成縮略圖的方法,涉及PHP的圖形操作及數(shù)學(xué)運算相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了PHP實現(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);
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》及《php常用函數(shù)與技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php提示W(wǎng)arning:mysql_fetch_array() expects的解決方法
這篇文章主要介紹了php提示W(wǎng)arning:mysql_fetch_array() expects的解決方法,是一個比較典型的php程序錯誤排查案例,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12

