首先需要安装Imagick扩展
1.php调用phpqrcode生成图片是索引模式,首先需要转换RGB模式
代码如下:
/**
* 转换RGB模式图片
* @param $filePath 图片路径
**/
public static function imagickPic($filePath){
// 首先生成二维码颜色值是索引,首先先转换成RGB
$image = new Imagick($filePath);
$image->stripImage();
$image->setImageColorspace(Imagick::COLORSPACE_RGB);
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
$image->setImageCompressionQuality(0);
$image->transformImageColorspace(12);
$image->setImageFormat("jpg");
// $filePath = '/mnt/www/ywym.veryman.xin/web/a/torgb.jpg';
$image->writeImage($filePath);
//附加: 调整图片分辨率以及大小
$this->reSizeJpg($filePath);
}
/**
* 调整图片分辨率以及图片尺寸大小
* @param $filePath 图片路径
* @param $width 图片宽度 201像素
* @param $height 图片高度 201像素
* 目前设置分辨率是300
**/
public static function reSizeJpg($filePath,$width =201,$height = 201)
{
ob_start(); // 打开缓冲区
$image = ob_get_contents();// 返回输出缓冲区的内容
ob_end_clean();// y/ 清空(檫除)缓冲区并关闭输出缓冲
$image_l = imagecreatefromjpeg($filePath);
$image_one = imageCreatetruecolor($width,$height);
$color = imagecolorallocate($image_one, 255, 255, 255);
imagefill($image_one, 0, 0, $color);
//
$arr = getimagesize($filePath);
imagecopyresampled($image_one,$image_l,0,0,0,0,$width,$height,$arr[0],$arr[1]);
imagejpeg($image_one, $filePath);
// 修改分辨率开始 ^
$file=$filePath;
$image = file_get_contents($file);// 把文件读入一个字符串
$images = substr_replace($image, pack("Cnn", 0x01, 300, 300), 13, 5);// 修改成 300
file_put_contents($filePath, $images);
}
转换完成之后
/**
* RGB图片转换CMYK图片
* @param $filePath 图片路径
**/
public function getimageCmyk($filePath){
$img = new Imagick($filePath);
$img->setImageColorSpace(Imagick::COLORSPACE_CMYK);
$icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
$img->profileImage('icc', $icc_rgb);
$img->negateImage(FALSE, Imagick::COLOR_CYAN);
$img->negateImage(FALSE, Imagick::COLOR_MAGENTA);
$img->negateImage(FALSE, Imagick::COLOR_YELLOW);
$img->negateImage(FALSE, Imagick::COLOR_BLACK);
unset($icc_rgb);
$img->stripImage(); // this will drop down the size of the image dramatically (removes all profiles)
// $filePath = '/mnt/www/ywym.veryman.xin/web/a/testrgb13.jpg';
$img->writeImage($filePath);
}
icc文件在附件当中