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);
?>

 

posted @ 2018-11-21 14:17  jianglingli  阅读(2681)  评论(0编辑  收藏  举报