前兩天要對一張圖片進行處理,其實很簡單,就是加上幾句話,一個圖片而已,但是自己如同得了短暫性失憶似的,圖片操作的函數一個都想不起來。所以就抽空整理了一下圖片操作函數。
圖片處理三步走:
創建畫布
從文件中創建一個新圖像
上面幾個函數區別在於圖片是什麼格式的,知道了圖片的格式也就能選對正確的函數了。
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//獲取圖片信息
list($width, $height, $type) = getimagesize($filename);
//創建一個畫布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$im = $createFun($f1);
圖片處理
圖片處理的函數就是參數多,具體說明還是看文檔的比較好!
$image , int $font , int $x , int $y , string $s , int $col )$dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )$dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
保存圖片並銷毀圖片
//保存圖片 $saveFun = 'image' . $type_arr[$type];
$saveFun($dst, $f2);
//銷毀圖片
imagedestroy($im); imagedestroy($dst);
第一步和第三步幾乎是固定的,拿來用就行了。
下面是自己寫的圖片處理函數
//imgText('images/xiaoming.jpg', 'xiaoming');
//imgWater('images/boluosha.jpg', 'images/water.png', 7);
imgThumb('images/boluosha.jpg', 100, 100, 'images/thumb.jpg');
function imgThumb($f1, $w, $h, $f2=''){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//獲取圖片信息
list($width, $height, $type) = getimagesize($f1);
//創建一個畫布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$im = $createFun($f1);
$dst = imagecreatetruecolor($w, $h);
//復制圖片
imagecopyresampled($dst, $im, 0, 0, 0, 0, $w, $h, $width, $height);
//保存圖片
if(empty($f2)) $f2 = $f1;
$saveFun = 'image' . $type_arr[$type];
$saveFun($dst, $f2);
//銷毀圖片
imagedestroy($im); imagedestroy($dst);
}
//在圖片上添加圖片
//$coord表示坐標,1左上角2右上角3左下角4右下角5上下居中6左右居中7全居中
function imgWater($f1, $f2, $coord=1, $f3=''){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//獲取圖片信息
list($w1, $h1, $t1) = getimagesize($f1);
list($w2, $h2, $t2) = getimagesize($f2);
//創建一個畫布
$createFun = 'imagecreatefrom' . $type_arr[$t1];
$im1 = $createFun($f1);
$createFun = 'imagecreatefrom' . $type_arr[$t2];
$im2 = $createFun($f2);
//復制圖片到另一張圖片上
$px = 0; $py=0;
switch($coord){
case 1 :break;
case 2 :
$px = $w1-$w2;
break;
case 3 :
$py = $h1-$h2;
break;
case 4:
$px = $w1-$w2; $py=$h1-$h2;
break;
case 5:
$py=($h1-$h2)/2;
break;
case 6:
$px = ($w1-$w2)/2;
break;
case 7:
$px = ($w1-$w2)/2; $py=($h1-$h2)/2;
break;
}
imagecopy($im1, $im2, $px, $py, 0, 0, $w2, $h2);
//保存圖片
if(empty($f3)){
$f3 = $f1;
}
$saveFun = 'image' . $type_arr[$t1];
$saveFun($im1, $f3);
//銷毀圖片
imagedestroy($im1); imagedestroy($im2);
}
//在圖片上添加文字/參數說明:文件路徑,字符串,顏色,位置x,位置y,字體1,2,3,4,5表示內置字體
function imgText($filename, $string, $fc='#F00', $px=0, $py=0, $fs=5){
$type_arr = array(1=>'gif', 2=>'jpeg', 3=>'png');
//獲取圖片信息
list($width, $height, $type) = getimagesize($filename);
//創建一個畫布
$createFun = 'imagecreatefrom' . $type_arr[$type];
$im = $createFun($filename);
//獲取顏色
list($r, $g, $b) = rgbtodec($fc);
$color = imagecolorallocate($im, $r, $g, $b);
//計算位置(默認居中)
if(empty($px) || empty($py)){
$px = ($width-imagefontwidth($fs) * strlen($string))/2;
$py = ($height-imagefontheight($fs))/2;
}
//寫入字符
imagestring($im, $fs, $px, $py, $string, $color);
//保存圖片
$saveFun = 'image' . $type_arr[$type];
$saveFun($im, $filename);
//銷毀畫布
imagedestroy($im);
}
//rgb值轉換十進制
function rgbtodec($str){
$str = str_replace('#', '', $str);
if(strlen($str)>4){
$r = substr($str, 0, 2);
$g = substr($str, 2, 2);
$b = substr($str, 4, 2);
}else{
$r = substr($str, 0, 1); $r .= $r;
$g = substr($str, 1, 1); $g .= $g;
$b = substr($str, 2, 1); $b .= $b;
}
return array(hexdec($r), hexdec($g), hexdec($b));
}