/**
* @comments: 缩放图片到一定比例,并且裁剪到一定比例
* @author: zhuanghuidong
* @time:
* @param $img_url 原图片url或者路径
* @param $pic_w 缩放宽度
* @param $pic_h 缩放高度
* @param $crop_w 裁剪宽度
* @param $crop_h 裁剪高度
*/
protected function crop_img($img_url, $pic_w, $pic_h, $crop_w, $crop_h)
{
$product_img_data = file_get_contents($img_url);
$info = getimagesize($img_url);
$product_img = imagecreatefromstring($product_img_data);
$src = imagecreatetruecolor($pic_w, $pic_h);
// 调整默认颜色
$color = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $color);
//imagecopy($src, $product_img, 0, 0, 100, 100, $pic_w, $pic_h);
imagecopyresampled($src, $product_img, 0, 0, 0, 0, $pic_w, $pic_h, $info[0], $info[1]);
$src1 = imagecreatetruecolor($crop_w, $crop_h);
$crop_w_length = round(($pic_w - $crop_w) / 2);
$crop_h_length = round(($pic_h - $crop_h) / 2);
// 调整默认颜色
imagefill($src1, 0, 0, $color);
imagecopy($src1, $src, 0, 0, $crop_w_length, $crop_h_length, $pic_w - $crop_w_length, $pic_h - $crop_h_length);
//imagejpeg($src1, $save_name, 100);
ob_start ();
imagejpeg($src1, null, 100);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode ($image_data);
//销毁零时图片资源
imagedestroy($src);
imagedestroy($src1);
return $image_data_base64;
}
/**
* @comments: 生成透明背景文字图片
* @author: zhuanghuidong
* @time:
* @param $text 文字
* @param $font_color 文字颜色
* @param $font_size 文字大小
* @param $font_weight 加粗
* @param $font_style 字体路径
*/
private function _set_water_text($text, $font_color, $font_size, $font_weight, $font_style)
{
//$text = iconv("gbk", "utf-8", $text);//转码,避免乱码
$rgb = explode(",", $font_color[1]);
$text_count = mb_strlen($text);
$block = imagecreatetruecolor($text_count * ($font_size + 15), $font_size + 20);//建立一个画板
$bg = imagecolorallocatealpha($block, 0, 0, 0, 127);//拾取一个完全透明的颜色,不要用imagecolorallocate拾色
$color = imagecolorallocate($block, $rgb[0], $rgb[1], $rgb[2]); //字体拾色
// imagealphablending($block, false);//关闭混合模式,以便透明颜色能覆盖原画板
imagefill($block, 0, 0, $bg);//填充
// imagefttext("Image", "字体大小", "旋转", "左边距","上边距", "字体颜色", "字体文件名称", "插入文本内容");
imagefttext($block, $font_size, 0, 10, $font_size + 10, $color, "." . $font_style, $text);
if ($font_weight == "bold") {
imagefttext($block, $font_size, 0, 11, $font_size + 10, $color, "." . $font_style, $text);
}
imagesavealpha($block, true);//设置保存PNG时保留透明通道信息
ob_start ();
imagepng($block,null,1);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode ($image_data);
imagedestroy($block);
return $image_data_base64;
}
/**
* @comments: 根据打标模板操作,给图片打标,并且上传到jd图片空间
* @author: zhuanghuidong
* @time:
* @param $product_img_data 待打标的图片
* @param $water_img_data_arr 水印图片数组
* @param int $pic_w
* @param int $pic_h
* @param array $text_arr 文字数组
* @param array $cate_id 图片空间分类id
* @return mixed
*/
public function water($product_img_data, $water_img_data_arr, $pic_w = 800, $pic_h = 800, $text_arr = array(), $cate_id)
{
$product_img = imagecreatefromstring($product_img_data);
$water_obj = array();
foreach ($water_img_data_arr as $key => $water) {
//将base64先进行解码
$water_obj[] = imagecreatefromstring(base64_decode($water));
}
for ($i = 0; $i < count($water_obj); $i++) {
imagealphablending($water_obj[$i], true);
}
$src = imagecreatetruecolor($pic_w, $pic_h);
// 调整默认颜色
$color = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $color);
imagecopy($src, $product_img, 0, 0, 0, 0, $pic_w, $pic_h);
for ($i = 0; $i < count($water_obj); $i++) {
imagecopy($src, $water_obj[$i], 0, 0, 0, 0, $pic_w, $pic_h);
}
foreach ($text_arr as $text_cfg) {
$rgb = explode(",", $text_cfg['rgb'][1]);
$font_size = $text_cfg['font_size'];
$position = $text_cfg['position'];
$font_style = $text_cfg['font_style'];
$text = $text_cfg['text'];
$font_weight = $text_cfg['font_weight'];
$color_text = imagecolorallocate($src, $rgb[0], $rgb[1], $rgb[2]); //字体拾色
// imagealphablending($block, false);//关闭混合模式,以便透明颜色能覆盖原画板
// imagefttext("Image", "字体大小", "旋转", "左边距","上边距", "字体颜色", "字体文件名称", "插入文本内容");
imagefttext($src, $font_size, 0, $position['width'], $position['height'], $color_text, "./Uploads/Fonts/" . $font_style, $text);
if ($font_weight == "bold") {
imagefttext($src, $font_size, 0, $position['width'] + 1, $position['height'], $color_text, "./Uploads/Fonts/" . $font_style, $text);
}
}
imagecopymerge($product_img, $src, 0, 0, 0, 0, $pic_w, $pic_h, 100);
// header('Content-Type:image/jpg');
//$file_name = "./Uploads/Fonts/Image/" . UID . time() . "tag_img.jpg";
//imagejpeg($product_img, $file_name, 100);
ob_start();
imagejpeg ($product_img,null,100);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode ($image_data);
$ret = $this->upload_pic($image_data_base64, time() . ".jpg", $cate_id);
//销毁零时图片资源
imagedestroy($product_img);
imagedestroy($src);
return $ret;
}