改变图片尺寸,可以支持jpg、png、gif

浏览:908 发布日期:2016/05/08 分类:技术分享
ThinkPHP 的图片类处理带动画的gif图片不正常,得自己写过一个支持jpg、png、gif的图片变换尺寸函数。

示意图:


代码:

<?php

$source_file = isset($_GET['source_file']) ? $_GET['source_file'] : '';
$new_file = isset($_GET['new_file']) ? $_GET['new_file'] : '';
$width = isset($_GET['width']) ? $_GET['width'] : 100;
$height = isset($_GET['height']) ? $_GET['height'] : 100;
$handle = isset($_GET['handle']) ? $_GET['handle'] : 1;





create_spec_image($source_file, $new_file, $width, $height, $handle);

/**
* 改变图片尺寸,可以支持jpg、png、gif;
* 如果是jpg图片,则进行补白,如果是png图片,则补充透明部分;
* 如果是gif图片,并且是动画图片,则生成不动的图片,但图片可用,不会变黑,也不会缺少。
* author: 郑爱军
* http://groupby.cn
* QQ:125315695
* 2016-05-08
*
* @param $source_file 原图片文件
* @param $new_file 新图片文件
* @param $newWidth 生成的宽度
* @param $newHeight 生成的高度
* @param $handle 操作类型:1为欠缺的部分进行补白;2为多出的部分切除
* @return 新生成的图片
*/

function create_spec_image($source_file, $new_file, $newWidth=100, $newHeight=100, $handle=1){

$image_info = getimagesize($source_file);

if($image_info === false){
return false;
}

$srcWidth = $image_info[0];
$srcHeight = $image_info[1];
$image_type = $image_info['mime'];

if($handle == 1){
//不够的部分补白。如果是jpg就加补白部分,如果是png就加透明部分。
$space = 'white';
}elseif($handle == 2){
//多出的部分截取
$space = 'cut';
}

//计算尺寸,开始================
//第一种情况:原图比例 = 新图比例,就不存在补白部分,也不存在切除部分。
if($srcWidth/$srcHeight == $newWidth/$newHeight){
$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newHeight;

$sourceX = 0;
$sourceY = 0;
$sourceW = $srcWidth;
$sourceH = $srcHeight;


//第二种情况:原宽高比 > 新宽高比,不够的部分进行补白。如果是jpg就补白,如果是png就补透明。例如:即原来的横扁,新的方正。
}elseif($srcWidth/$srcHeight > $newWidth/$newHeight && $space=='white'){
$beginX = 0;
$beginY = ($newHeight - $newWidth/$srcWidth * $srcHeight)/2;
if($beginY < 0){
$beginY = 0;
}
$endX = $newWidth;
$endY = $newWidth/$srcWidth * $srcHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;

//第三种情况:原宽高比 < 新宽高比,不够的部分进行补白。如果是jpg就补白,如果是png就补透明。例如:,即原来的长窄,新的方正。
}elseif($srcWidth/$srcHeight < $newWidth/$newHeight && $space=='white'){
$beginY = 0;
$beginX = ($newWidth - $newHeight*$srcWidth / $srcHeight)/2;
if($beginX < 0){
$beginX = 0;
}
$endY = $newHeight;
$endX = $newHeight*$srcWidth / $srcHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;


//第四种情况:原宽高比 > 新宽高比,则切除上下部分。例如:原来的横扁,新的方正。
}elseif($srcWidth/$srcHeight > $newWidth/$newHeight && $space=='cut'){
$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newHeight;

$sourceY = 0;
$sourceX = ($srcWidth - $srcHeight * $newWidth/$newHeight)/2;
if($sourceX < 0){
$sourceX = 0;
}
$sourceH = $srcHeight;
$sourceW = $srcHeight * $newWidth/$newHeight;

//第五种情况:原宽高比 < 新宽高比,则切除左右部分。例如:即原来的长窄,新的方正。
}elseif($srcWidth/$srcHeight < $newWidth/$newHeight && $space=='cut'){
$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newHeight;

$sourceX = 0;
$sourceY = ($srcHeight - $srcWidth / $newWidth * $newHeight)/2;
if($sourceY < 0){
$sourceY = 0;
}
$sourceW = $srcWidth;
$sourceH = $srcWidth / $newWidth * $newHeight;
}
//计算尺寸,结束================


//生成图片,开始==============
if($image_type == 'image/jpeg'){
$srcImg = imagecreatefromjpeg($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
$color = imagecolorallocate($created_image, 255, 255, 255); //指定为白色
imagefill($created_image, 0, 0, $color); //填充白色

if(function_exists("imagecopyresampled")){
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
}else{
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
}
//imageinterlace($created_image, $interlace=1); //渐进式显示。$interlace=0为基本形式,baseline JPEG; $interlace=1为渐进式,Progressive JPEG
imagejpeg($created_image, $new_file, 100);

}elseif($image_type == 'image/png'){
$srcImg = imagecreatefrompng($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($created_image, false);
$color = imagecolorallocatealpha($created_image, 0, 0, 0, 127);
imagefill($created_image, 0, 0, $color);
imagesavealpha($created_image, true);
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
imagepng($created_image, $new_file);

}elseif($image_type == 'image/gif'){
$srcImg = imagecreatefromgif($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
$color=imagecolorallocate($created_image,255,255,255);
imagecolortransparent($created_image, $color);
imagefill($created_image,0,0,$color);
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
imagegif($created_image, $new_file, 100);
}
//生成图片,结束==============

imagedestroy($created_image);
imagedestroy($srcImg);

}



======== 新的写法,增加不补白、不剪切的情况,共 6 种 2016-05-09 ========
<?php

$source_file = isset($_GET['source_file']) ? $_GET['source_file'] : '';
$new_file = isset($_GET['new_file']) ? $_GET['new_file'] : '';
$width = isset($_GET['width']) ? $_GET['width'] : 100;
$height = isset($_GET['height']) ? $_GET['height'] : 100;
$handle = isset($_GET['handle']) ? $_GET['handle'] : 1;





create_spec_image($source_file, $new_file, $width, $height, $handle);

/**
* 改变图片尺寸,可以支持jpg、png、gif;
* 如果是jpg图片,则进行补白,如果是png图片,则补充透明部分;
* 如果是gif图片,并且是动画图片,则生成不动的图片,但图片可用,不会变黑,也不会缺少。
* author: 郑爱军
* http://groupby.cn
* QQ:125315695
* 2016-05-09
*
* @param $source_file 原图片文件
* @param $new_file 新图片文件
* @param $newWidth 生成的宽度
* @param $newHeight 生成的高度
* @param $handle 操作类型:1为欠缺的部分进行补白;2为多出的部分剪切掉;3为不补白、也不剪切,自动根据新的宽高来决定。
* @return 新生成的图片,总共出现 6 种情况。
*/

function create_spec_image($source_file, $new_file, $newWidth=100, $newHeight=100, $handle=1){

$image_info = getimagesize($source_file);

if($image_info === false){
return false;
}

$srcWidth = $image_info[0];
$srcHeight = $image_info[1];
$image_type = $image_info['mime'];

if($handle == 1){
//不够的部分补白。如果是jpg就加补白部分,如果是png就加透明部分。
$space = 'white';
}elseif($handle == 2){
//多出的部分截取
$space = 'cut';
}elseif($handle == 3){
$space = 'none';
}else{
$space = 'white';
}

//计算尺寸,开始================
//第一种情况:不补白、也不剪切,根据宽高比决定。此时由新高度决定新图。
if($srcWidth/$srcHeight >= $newWidth/$newHeight && $space == 'none'){

$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newWidth / $srcWidth * $srcHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;

$newHeight = $endY;

//第二种情况:不补白、也不剪切,根据宽高比决定。此时由新宽度决定新图。
}elseif($srcWidth/$srcHeight < $newWidth/$newHeight && $space == 'none'){

$beginX = 0;
$beginY = 0;
$endX = $newHeight / $srcHeight * $srcWidth;
$endY = $newHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;

$newWidth = $endX;

//第三种情况:原宽高比 > 新宽高比,不够的部分进行补白。如果是jpg就补白,如果是png就补透明。例如:即原来的横扁,新的方正。
}elseif($srcWidth/$srcHeight >= $newWidth/$newHeight && $space=='white'){
$beginX = 0;
$beginY = ($newHeight - $newWidth/$srcWidth * $srcHeight)/2;
if($beginY < 0){
$beginY = 0;
}
$endX = $newWidth;
$endY = $newWidth/$srcWidth * $srcHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;

//第四种情况:原宽高比 < 新宽高比,不够的部分进行补白。如果是jpg就补白,如果是png就补透明。例如:,即原来的长窄,新的方正。
}elseif($srcWidth/$srcHeight < $newWidth/$newHeight && $space=='white'){
$beginY = 0;
$beginX = ($newWidth - $newHeight*$srcWidth / $srcHeight)/2;
if($beginX < 0){
$beginX = 0;
}
$endY = $newHeight;
$endX = $newHeight*$srcWidth / $srcHeight;

$sourceX = 0;
$sourceY = 0;
$sourceH = $srcHeight;
$sourceW = $srcWidth;


//第五种情况:原宽高比 > 新宽高比,则剪切上下部分。例如:原来的横扁,新的方正。
}elseif($srcWidth/$srcHeight >= $newWidth/$newHeight && $space=='cut'){
$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newHeight;

$sourceY = 0;
$sourceX = ($srcWidth - $srcHeight * $newWidth/$newHeight)/2;
if($sourceX < 0){
$sourceX = 0;
}
$sourceH = $srcHeight;
$sourceW = $srcHeight * $newWidth/$newHeight;

//第六种情况:原宽高比 < 新宽高比,则剪切左右部分。例如:即原来的长窄,新的方正。
}elseif($srcWidth/$srcHeight < $newWidth/$newHeight && $space=='cut'){
$beginX = 0;
$beginY = 0;
$endX = $newWidth;
$endY = $newHeight;

$sourceX = 0;
$sourceY = ($srcHeight - $srcWidth / $newWidth * $newHeight)/2;
if($sourceY < 0){
$sourceY = 0;
}
$sourceW = $srcWidth;
$sourceH = $srcWidth / $newWidth * $newHeight;
}
//计算尺寸,结束================


//生成图片,开始==============
if($image_type == 'image/jpeg'){
$srcImg = imagecreatefromjpeg($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
$color = imagecolorallocate($created_image, 255, 255, 255); //指定为白色
imagefill($created_image, 0, 0, $color); //填充白色

if(function_exists("imagecopyresampled")){
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
}else{
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
}
//imageinterlace($created_image, $interlace=1); //渐进式显示。$interlace=0为基本形式,baseline JPEG; $interlace=1为渐进式,Progressive JPEG
imagejpeg($created_image, $new_file, 100);

}elseif($image_type == 'image/png'){
$srcImg = imagecreatefrompng($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($created_image, false);
$color = imagecolorallocatealpha($created_image, 0, 0, 0, 127);
imagefill($created_image, 0, 0, $color);
imagesavealpha($created_image, true);
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
imagepng($created_image, $new_file);

}elseif($image_type == 'image/gif'){
$srcImg = imagecreatefromgif($source_file);
$created_image = imagecreatetruecolor($newWidth, $newHeight);
$color=imagecolorallocate($created_image,255,255,255);
imagecolortransparent($created_image, $color);
imagefill($created_image,0,0,$color);
imagecopyresampled($created_image, $srcImg, $beginX, $beginY, $sourceX, $sourceY, $endX, $endY, $sourceW, $sourceH);
imagegif($created_image, $new_file, 100);
}
//生成图片,结束==============

imagedestroy($created_image);
imagedestroy($srcImg);

}




原文地址:

http://qp500.cn/groupby/Blog/16677
最佳答案
评论( 相关
后面还有条评论,点击查看>>