php圖片縮放實(shí)現(xiàn)方法
php基礎(chǔ)練習(xí)--圖片縮放:
<?php
/**
* image zoom.
*/
function imageZoom($filename, $w, $h) {
/* Arguments meaning */
/* $filename: the source of the name */
/* $w: you want get the image's width */
/* $h: you want get the imgage's height */
$arr = getimagesize($filename);
$src_w = $arr[0];
$src_h = $arr[1];
$src_t = $arr[2];
/*1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),
= TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,
= IFF,15 = WBMP,16 = XBM*/
$src_m = $arr['mime'];
$src_img = imagecreatefromjpeg($filename);
if (($w / $src_w) >($h / $src_h)) {
$bili = $h / $src_h;
} else {
$bili = $w / $src_h;
}
$dst_w = $src_w * $bili;
$dst_h = $src_h * $bili;
$dst_img = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
header("content-type:{$src_m}");
switch ($src_t) {
case 1:
$imgout = "imagegif";
break;
case 2:
$imgout = "imagejpeg";
break;
case 3:
$imgout = "imagepng";
break;
default:
echo "The type was wrong!";
break;
}
$dst_filename = "s_".$filename;
$imgout($dst_img, $dst_filename);
imagedestroy($dst_img);
}
$filename = 'gg.jpg';
imageZoom($filename, 100, 200);
核心:<1>注意縮放比例如何得到,雖然這樣得到的圖片可能會(huì)與預(yù)想的有點(diǎn)差別,但是最起碼保證了縮放比例。
<2>類型的控制。
相關(guān)文章
ThinkPHP字符串函數(shù)及常用函數(shù)匯總
這篇文章主要介紹了ThinkPHP字符串函數(shù)及常用函數(shù)匯總,可供開發(fā)人員參考使用,需要的朋友可以參考下2014-07-07PHP 枚舉類型的管理與設(shè)計(jì)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于PHP 枚舉類型的管理與設(shè)計(jì)知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)參考下。2020-02-02ThinkPHP模板循環(huán)輸出Volist標(biāo)簽用法實(shí)例詳解
這篇文章主要介紹了ThinkPHP模板循環(huán)輸出Volist標(biāo)簽用法,結(jié)合實(shí)例形式詳細(xì)分析了Volist標(biāo)簽的功能,使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03PHP表單提交后引號(hào)前自動(dòng)加反斜杠的原因及三種辦法關(guān)閉php魔術(shù)引號(hào)
一般空間商提供的服務(wù)器空間默認(rèn)PHP 指令 magic_quotes_gpc是on的,也就是打開的。我們通常用stripslashes() 函數(shù)刪除自動(dòng)添加的反斜杠。2015-09-09Laravel 使用查詢構(gòu)造器配合原生sql語(yǔ)句查詢的例子
今天小編就為大家分享一篇Laravel 使用查詢構(gòu)造器配合原生sql語(yǔ)句查詢的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析
這篇文章主要介紹了Thinkphp5.0 框架視圖view的比較標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架eq、equal、neq、notequal、egt及switch、range、between等標(biāo)簽相關(guān)用法,需要的朋友可以參考下2019-10-10