由于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输出图形;