Regarding Two image merge using php merge

Discussion in 'PHP' started by LokeshSingh, May 4, 2012.

  1. #1
    I want to merge two image one is temp image which is recent uploaded and one comes from drag drop.

    But it display error Warning: imagecreatefromjpeg
    Warning: imagesx(): supplied argument is not a valid Image resource in...

    Please suggest the solution
     
    LokeshSingh, May 4, 2012 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Can you show us the code? so we can see what's wrong
     
    Oli3L, May 5, 2012 IP
  3. LokeshSingh

    LokeshSingh Active Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    <?php
    $res = json_decode(stripslashes($_POST['jsondata']), true);
    /* get data */
    $count_images = count($res['images']);
    /* the background image is the first one */
    $background = $res['images'][0]['src'];
    $photo1 = imagecreatefromjpeg($background);
    $foto1W = imagesx($photo1);
    $foto1H = imagesy($photo1);
    $photoFrameW = $res['images'][0]['width'];
    $photoFrameH = $res['images'][0]['height'];
    $photoFrame = imagecreatetruecolor($photoFrameW,$photoFrameH);
    imagecopyresampled($photoFrame, $photo1, 0, 0, 0, 0, $photoFrameW, $photoFrameH, $foto1W, $foto1H);

    /* the other images */
    for($i = 1; $i < $count_images; ++$i){
    $insert = $res['images'][$i]['src'];
    $photoFrame2Rotation = (180-$res['images'][$i]['rotation']) + 180;

    $photo2 = imagecreatefrompng($insert);

    $foto2W = imagesx($photo2);
    $foto2H = imagesy($photo2);
    $photoFrame2W = $res['images'][$i]['width'];
    $photoFrame2H = $res['images'][$i]['height'];

    $photoFrame2TOP = $res['images'][$i]['top'];
    $photoFrame2LEFT= $res['images'][$i]['left'];

    $photoFrame2 = imagecreatetruecolor($photoFrame2W,$photoFrame2H);
    $trans_colour = imagecolorallocatealpha($photoFrame2, 0, 0, 0, 127);
    imagefill($photoFrame2, 0, 0, $trans_colour);

    imagecopyresampled($photoFrame2, $photo2, 0, 0, 0, 0, $photoFrame2W, $photoFrame2H, $foto2W, $foto2H);

    $photoFrame2 = imagerotate($photoFrame2,$photoFrame2Rotation, -1,0);
    /*after rotating calculate the difference of new height/width with the one before*/
    $extraTop =(imagesy($photoFrame2)-$photoFrame2H)/2;
    $extraLeft =(imagesx($photoFrame2)-$photoFrame2W)/2;

    imagecopy($photoFrame, $photoFrame2,$photoFrame2LEFT-$extraLeft, $photoFrame2TOP-$extraTop, 0, 0, imagesx($photoFrame2), imagesy($photoFrame2));
    }
    // Set the content type header - in this case image/jpeg
    header('Content-type: image/jpeg');
    imagejpeg($photoFrame, $targetfile);
    imagedestroy($photoFrame);
    ?>

    And temp image generated and exist in /folder and use as background.
    But after merge this join drag image + background.
    background image position is root like sample.jpg
    i dont want i want combined image from folder as temp.
     
    LokeshSingh, May 6, 2012 IP
  4. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #4
    $background     = $res['images'][0]['src'];
    
    PHP:

    Could you tell what is the exact value of background?

    From the code, it looks like html source, but html source can be relative (like src="/images/target.png") and then it does not work with imagecreatefromjpeg function.
    Also are you sure, that $background is alwasy JPEG image?
    Are you certain that url_fopen_wrapper is enabled in your server?
     
    e-abi, May 7, 2012 IP