標籤:
PHP 拷貝映像 imagecopy 與 imagecopyresized 函數
imagecopy() 函數用於拷貝映像或映像的一部分。
imagecopyresized() 函數用於拷貝部分映像並調整大小。
imagecopy()
imagecopy() 函數用於拷貝映像或映像的一部分,成功返回 TRUE ,否則返回 FALSE 。
文法:
bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,
int src_w, int src_h )
參數說明:參數說明
dst_im靶心圖表像
src_im被拷貝的源映像
dst_x靶心圖表像開始 x 座標
dst_y靶心圖表像開始 y 座標,x,y同為 0 則從左上方開始
src_x拷貝映像開始 x 座標
src_y拷貝映像開始 y 座標,x,y同為 0 則從左上方開始拷貝
src_w(從 src_x 開始)拷貝的寬度
src_h(從 src_y 開始)拷貝的高度
例子:
<?php
header("Content-type: image/jpeg");
//建立靶心圖表像
$dst_im = imagecreatetruecolor(150, 150);
//源映像
$src_im = @imagecreatefromjpeg("images/flower_1.jpg");
//拷貝源映像左上方起始 150px 150px
imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );
//輸出拷貝後映像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>
1 <?php 2 //這裡僅僅是為了案例需要準備一些素材圖片 3 $url = ‘http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg‘; 4 $content = file_get_contents($url); 5 $filename = ‘tmp.jpg‘; 6 file_put_contents($filename, $content); 7 $url = ‘http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png‘; 8 file_put_contents(‘logo.png‘, file_get_contents($url)); 9 //開始添加浮水印操作10 $im = imagecreatefromjpeg($filename);11 $logo = imagecreatefrompng(‘logo.png‘);12 $size = getimagesize(‘logo.png‘);13 imagecopy($im, $logo, 600, 150, 0, 00, $size[0], $size[1]);14 15 header("content-type: image/jpeg");16 imagejpeg($im);17 ?>
imagecopyresized()
imagecopyresized() 函數用於拷貝映像或映像的一部分並調整大小,成功返回 TRUE ,否則返回 FALSE 。
文法:
bool imagecopyresized( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,
int dst_w, int dst_h, int src_w, int src_h )
本函數參數可參看 imagecopy() 函數,只是本函數增加了兩個參數(注意順序):
dst_w:靶心圖表像的寬度。
dst_h:靶心圖表像的高度。
imagecopyresized() 的典型應用就是產生圖片的縮圖:
<?php
header("Content-type: image/jpeg");
//原圖檔案
$file = "images/flower_1.jpg";
// 縮圖比例
$percent = 0.5;
// 縮圖尺寸
list($width, $height) = getimagesize($file);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 載入映像
$src_im = @imagecreatefromjpeg($file);
$dst_im = imagecreatetruecolor($newwidth, $newheight);
// 調整大小
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//輸出縮小後的映像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>
PHP給圖片添加浮水印