转自:
:请宝姐姐喝茶
素材:
下面几个库是必不可少的
endroid/qrcode //如果需要一个二维码的话 sybio/image-workshop sybio/gif-creator sybio/gif-frame-extractor
好,进入正题,直接用代码说话
setSize(100); //设置大小,单位像素$qr_resource = imagecreatefromstring($qrCode->writeString()); //生成一个二维码图片资源$qr_image = ImageWorkshop::initFromResourceVar($qr_resource); //设置一个二维码图层$image_info = getimagesize($background); //图片的长宽参数$tea = ImageWorkshop::initFromPath($tea_image); //喝茶图层//$tea = ImageWorkshop::initTextLayer('呵呵', 'simkai.ttf', 15); //假如希望在图片上写一行文字if (GifFrameExtractor::isAnimatedGif($background)) { //底图如果是gif $extractor = new GifFrameExtractor(); $frames = $extractor->extract($background, false); //gif图拆成帧,第二个参数只有对原图有万分保证,质量极佳的情况下才应该设置为true foreach ($frames as $frame) { //逐帧处理 $frame_layer = ImageWorkshop::initFromResourceVar($frame['image']); //帧图层 $frame_layer->resizeInPixel($image_info[0], $image_info[1], true); // 设置每一帧的高度宽度 $frame_layer->addLayerOnTop($tea, 0, 0, 'LB'); // 添加喝茶图层,位置距离左下角坐标为 0,0 $frame_layer->addLayerOnTop($qr_image, 0, 0, 'RB'); //在背景图层上叠加自定义二维码,距离右下角坐标为 0,0 $new_frames[] = $frame_layer->getResult(); //逐帧入栈 } $creator = new GifCreator(); $creator->create($new_frames, $extractor->getFrameDurations(), 0); //我们需要一直循环 $new_gif = $creator->getGif(); //将新的帧序列创建为新的GIF图片 $file_name = '请宝姐姐喝茶_'. time() . '.gif'; file_put_contents('images/target/' . $file_name, $new_gif); //简单粗暴的将图片字节写入文件}
打完收工