使用PHP縮圖跟剪下圖

來源:互聯網
上載者:User
使用PHP縮圖和剪下圖
API:
resource imagecreatetruecolor ( int $width , int $height )
magecreatetruecolor() 返回一個映像標識符,代表了一幅大小為 x_size 和 y_size 的黑色映像。

是否定義了本函數取決於 PHP 和 GD 的版本。從 PHP 4.0.6 到 4.1.x 只要載入了 GD 模組本函數一直存在,但是在沒有安裝 GD2 的時候調用,PHP 將發出致命錯誤並退出。在 PHP 4.2.x 中此行為改為發出警告而不是錯誤。其它版本只在安裝了正確的 GD 版本時定義了本函數。

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 )
將 src_im 映像中座標從 src_x,src_y 開始,寬度為 src_w,高度為 src_h 的一部分拷貝到 dst_im 映像中座標為 dst_x 和 dst_y 的位置上。

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() 從 image 映像以 filename 為檔案名稱建立一個 JPEG 映像。

ool imagedestroy ( resource $image )
imagedestroy() 釋放與 image 關聯的記憶體。image 是由映像建立函數返回的映像標識符,例如 imagecreatetruecolor()。



http://www.cnblogs.com/xiaomia/archive/2010/11/13/1876191.html
一開始採用了 imagecopyresized 方法進行映像等比縮小,實際操作後發現,映像縮小後燥點非常嚴重。後再換用 imagecopysampled 方法,該方法會對映像進行重新採樣,對縮小的映像進行平滑處理,使清晰度得到很大提高
list($src_w,$src_h)=getimagesize($src_img);  // 擷取原圖尺寸$dst_scale = $dst_h/$dst_w; //靶心圖表像長寬比$src_scale = $src_h/$src_w; // 原圖長寬比if($src_scale>=$dst_scale){  // 過高$w = intval($src_w);$h = intval($dst_scale*$w);$x = 0;$y = ($src_h - $h)/3;}else{ // 過寬$h = intval($src_h);$w = intval($h/$dst_scale);$x = ($src_w - $w)/2;$y = 0;}// 剪裁$source=imagecreatefromjpeg($src_img);$croped=imagecreatetruecolor($w, $h);imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);// 縮放$scale = $dst_w/$w;$target = imagecreatetruecolor($dst_w, $dst_h);$final_w = intval($w*$scale);$final_h = intval($h*$scale);imagecopyresampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);// 儲存$timestamp = time();imagejpeg($target, "$timestamp.jpg");imagedestroy($target);


http://www.cnblogs.com/analyzer/articles/1267017.html
先說說縮圖,它用得比較多,代碼如下:
 



再說說剪下圖,就是不縮放,而是從原圖中剪下出一塊小圖,比較個性。代碼如下:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.