duandou1903 2013-11-21 02:09
浏览 61
已采纳

尝试使用PHP将png合并到jpg上

I am trying to merge two images using PHP. One is a jpg that I have pulled in the form of a Facebook URL, the other is a "watermark" logo that is a png that I hope to apply in the bottom right hand corner of the Facebook image before posting the image back to Facebook.

I'm having trouble merging the two images. The function below is passed the url of the Facebook image (which is a jpg) $imagedata = the Facebook image URL. The function is currently returning: Resource id#1. I am hoping to save the merged image to the /uploads/ folder as the same previously randomly generated filename and return it's location from the function.

I have previously referenced this page: Merge a png ontop of a jpg and retain transparency with php

function createImage($imagedata) {

    $upload_dir = ($_SERVER['DOCUMENT_ROOT'] .'/uploads/');

    $filename =generateFilename().'.jpg'; //generateFilename() does nothing more than create a random string to use as a unique filename

    $filelocation=$upload_dir . $filename;

    file_put_contents($filelocation,file_get_contents($imagedata));

    $localimage = $filename;

    $dest = imagecreatefromjpeg('/uploads/' . $localimage);

    $src = imagecreatefrompng('images/overlay.png');

    imagecopyresampled($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, $src2w, $src2h); 

    //header('Content-Type: image/png');

    imagejpeg($dest, 'dickshlitz.jpg', 100);

    imagedestroy($dest);

    imagedestroy($src);            }

Any and all help is deeply appreciated. Please ask questions if I have not been clear enough.

  • 写回答

1条回答 默认 最新

  • dtg25862 2013-11-22 19:56
    关注

    Okay, I got this working. My first issue was that I was confused by the output of imagecopyresampled() because I was trying to print it, which isn't what it's designed to do. At the time of posting the initial question I hadn't noticed the .jpg file was being output to my server, so really the code was already doing its job.

    However, once I noticed the output and inspected it I noticed it wasn't functioning as expected. This was the fault of my not understanding imagecopyresampled() and passing it variables that hadn't been declared. After much tinkering the working code is below.

    $dest = imagecreatefromjpeg('background.jpg');
    $src = imagecreatefrompng('watermark.png');
    
        imagesavealpha($src, true); 
        imagealphablending($src, true);
        imagesavealpha($dest, true); 
        imagealphablending($dest, true);
        list($newwidth, $newheight, $type, $attr) = getimagesize('overlay.png');
    
        imagecopyresampled($dest, $src, 200 , 100, 0, 0, $newwidth , $newheight, $newwidth , $newheight); 
    
        imagepng($dest, $localimage);
    
        imagedestroy($dest);
        imagedestroy($src);
    

    If anyone can make commnent to improve this code I would appreciate it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答