php 圖片剪裁imagecopyresized()和imagecopyresampled();


2018-11-21

主要說明下幾個用到的函數:

imagecreatefromjpeg() 返回一圖像標識符,代表了從給定的文件名取得的圖像。

int imagesx ( resource image) 返回 image 所代表的圖像的寬度。

int imagesy ( resource image) 返回 image 所代表的圖像的高度。

bool function_exists ( string function_name)   定義指定的函數則返回 true 值,其它情形均返回 false 值。

imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。

imagecreate() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的空白圖像。

int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)

imagecopyresized 與上面方法相同,區別見下面說明:
與上面的參數相同

 

ImageCopyResized()函數在所有GD版本中有效,但其縮放圖像的算法比較粗糙。

imagecopyresampled(),其像素插值算法得到的圖像邊緣比較平滑,但該函數的速度比ImageCopyResized()慢。


ImageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

ImageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

bool imagecopyresampled ( resource $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 )

int srcW, int srcH)   重采樣拷貝部分圖像並調整大小

$dst_image:新建的圖片

$src_image:需要載入的圖片

$dst_x:設定需要載入的圖片在新圖中的x坐標

$dst_y:設定需要載入的圖片在新圖中的y坐標

$src_x:設定載入圖片要載入的區域x坐標

$src_y:設定載入圖片要載入的區域y坐標

$dst_w:設定載入的原圖的寬度(在此設置縮放)

$dst_h:設定載入的原圖的高度(在此設置縮放)

$src_w:原圖要載入的寬度

$src_h:原圖要載入的高度

 

imagejpeg() 從 image 圖像以 filename 為文件名創建一個 JPEG 圖像。

imagedestroy() 釋放與 image 關聯的內存。

 

    /******
     * 上傳圖片
     */
    public static function upload_image($src_image, $wide, $high, $sql, $is_ipad=false) {    //$upfile 上傳文件
        
        if (!file_exists($sql['dir'] . '/' . $sql['file_name'])) {
            //寫入文件
            $srcW = ImageSX($src_image); //獲得圖片寬
            $srcH = ImageSY($src_image); //獲得圖片高
            $dst_image = ImageCreateTrueColor($wide,$high);
            $result = ImageCopyResized($dst_image, $src_image, 0, 0, 0, 0, $wide, $high, $srcW, $srcH);
            if (!ImageJpeg($dst_image, $sql['dir'] . '/' . $sql['file_name'],75)) 
                FUNC::re('445102', $sql);
        } else {
            unlink($sql['dir'] . '/' . $sql['file_name']);
            $srcW = ImageSX($src_image); //獲得圖片寬
            $srcH = ImageSY($src_image); //獲得圖片高
            $dst_image = ImageCreateTrueColor($wide,$high);
            $result = ImageCopyResized($dst_image, $src_image, 0, 0, 0, 0, $wide, $high, $srcW, $srcH);
            if (!ImageJpeg($dst_image, $sql['dir'] . '/' . $sql['file_name'],75)) 
                FUNC::re('445102', $sql);
        }
        //返回數據 ipad點餐返回數據資源
        if($is_ipad){

            $content=file_get_contents($sql['dir'] . '/' . $sql['file_name']);
            //$content=bin2hex($content);

            return $content;
        }
        //返回數據
        return true;
    }

 

 如果對縮略圖的質量要求不高可以使用imagecopyresized()函數,imagecopyresize()所生成的圖像比較粗糙,但是速度較快;imagecopyresampled()函數是GD 2.x后新增加的函數,字面上的意思是會對圖片進行重新采樣(resampling),GD是采用插算法生成更平滑的圖像,但是速度相對imagecopyresize()函數來說要慢一些。

使用imagecopyresized()將圖片縮小一半

代碼:

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>

改變后的圖片:



使用imagecopyresampled()將圖片縮小一半

代碼:

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM