php缩率图

由于ios需要几个分辨率的桌面图标,但图标都一样,只有分辨率不一样,所以可以用缩率图的方式保存为多个分辨率的图标,上传时上传最大的分辨率,然后用imagecopyresampled方法或imagecopyresized方法缩率图片就可以了。不同的是imagecopyresized所生成的图片比较粗糙,所以我用的是imagecopyresampled;

示例代码如下:


/**

*$imgform:图片来源

*$imgnewwidth:生成的图片宽度

*$imgnewheight:生成的图片高度

*/

function _make_thumb($imgform, $imgnewwidth, $imgnewheight)
{
$imginfo = getimagesize($imgform);
//获取图片格式
switch($imginfo[2])
{
case 1:
$newimg = imagecreatefromgif($imgform);
break;
case 2:
$newimg = imagecreatefromjpeg($imgform);
break;
case 3:
$newimg = imagecreatefrompng($imgform);
break;
default:
$this->show_warning('上传类型错误!');

break;
}

//获取图片高宽
$imgoldwidth = imagesx($newimg);
$imgoldheight = imagesy($newimg);
if ($imgoldwidth>200 || $imgoldheight>200)
{
$this->show_warning('请上传200*200像素的图片!');
}
$newimgcolor = imagecreatetruecolor($imgnewwidth, $imgnewheight);
imagecopyresampled($newimgcolor, $newimg, 0, 0, 0, 0, $imgnewwidth, $imgnewheight, $imgoldwidth, $imgoldheight);


imagepng($newimgcolor,'data/files/store_' . $this->_store_id . '/other'.'/store_logo'.$imgnewheight.'.png');



imagedestroy($newimg);
return 'data/files/store_' . $this->_store_id . '/other'.'/store_logo'.$imgnewheight.'.png';
}


getimagesize:返回的数组有四个元素。返回数组的第一个元素 (索引值 0) 是图片的高度,单位是像素 (pixel)。第二个元素 (索引值 1) 是图片的宽度。第三个元素 (索引值 2) 是图片的文件格式,其值 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式。第四个元素 (索引值 3) 为图片的高与宽字符串,height=xxx width=yyy。

imagecreatefromgif、imagecreatefromjpeg、imagecreatefrompng函数用来取出一张 对应格式图形;

imagesx、imagesy获取图片宽高;

imagecreatetruecolor  新建一个真彩色图像;

imagepng输出图形;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值