使用 Imagick
$public_path = ROOT_PATH . 'public'; $good_img_path = $public_path . $product['cover_image']; if (!file_exists($good_img_path)) { $slide_images = $product['slide_images'] ? explode(',', $product['slide_images']) : []; $good_img_path = count($slide_images) > 0 ? $slide_images[0] : ''; $good_img_path = $public_path . $good_img_path; } if (!file_exists($good_img_path)) { return -2; } // 背景图片 $bj_path = ROOT_PATH . '/public/uploads/bg.jpg';//目标大图,背景图的链接 $bgImage = new \Imagick($bj_path); $bgHeight = $bgImage->getImageHeight(); // 上方文字 $font_file = ROOT_PATH . "public/assets/fonts/msyh.ttc"; $text = sprintf('%s 人 团 中 %s 人', $product['max_group_num'], $product['winner_num']); $draw = new \ImagickDraw(); $draw->setFontSize('40'); $draw->setTextKerning(1); // 设置文件间距 $draw->setFont($font_file); $draw->setFontWeight(200); // 字体粗体 $draw->setFillColor('#ffffff'); // 字体颜色 $draw->setFontFamily("Palatino"); $draw->setGravity(\Imagick::GRAVITY_NORTH); $bgImage->annotateImage($draw, 0, 18, 0, $text); // 商品图片 // $good_img_path = ROOT_PATH . 'public/uploads/goods1.png'; $goodImage = new \Imagick($good_img_path); $goodImage->thumbnailImage(233, 230); $bgImage->compositeImage($goodImage, \Imagick::COMPOSITE_DEFAULT , 18, 102); // 商品价格 $text1 = sprintf("¥%s", $product['deposit']); $draw = new \ImagickDraw(); $draw->setFontSize('50'); $draw->setTextKerning(1); // 设置文件间距 $draw->setFont($font_file); $draw->setFontWeight(100); // 字体粗体 $draw->setFillColor('#f3425e'); // 字体颜色 243, 66, 94 $draw->setFontFamily("Palatino"); $draw->setGravity(\Imagick::GRAVITY_NORTH); $bgImage->annotateImage($draw, 114, 144, 0, $text1); // 底部文字 $text2 = sprintf("拼中得商品 | 未拼中返%s元", $product['compensation']); $draw = new \ImagickDraw(); $draw->setFontSize('24'); $draw->setTextKerning(1); // 设置文件间距 $draw->setFont($font_file); $draw->setFontWeight(100); // 字体粗体 $draw->setFillColor('#FFFFFF'); // 字体颜色 $draw->setFontFamily("Palatino"); $draw->setGravity(\Imagick::GRAVITY_NORTH); $bgImage->annotateImage($draw, 0, $bgHeight - 44, 0, $text2); $file_path = '/uploads/shareimgs/share-' . uniqid() . '.png'; $bgImage->writeImage(ROOT_PATH . 'public/' . $file_path);
评论
评论功能已经关闭!