-1
<img id='imgt' src='../img/crop00.jpg' alt='img'>
<img id='imgtest' src='../img/crop00.jpg' alt='img'>

js

...
$.ajax({
    url: 'pro20.php',
    type: 'post',
    data: {...},
    success: function(data) {
        $('#imgtest').attr('src', data);
    }
});

php

error_reporting(E_ALL);
ini_set('display_errors', 1);

GD library is turned on - checked with print_r(gd_info());

pro20.php

I'm trying to run this code

$stamp = imagecreatefrompng('img/wmark.png');
$im = imagecreatefromjpeg('img/test.jpg');

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

echo imagepng($im);  // my added line 28

Result:
- #imgtest is blank,
- in file system there is nowhere new img created
- error log:

[:error] [pid 2736:tid 1580] [client ::1:59185] PHP Warning: imagepng(): supplied resource is not a valid Image resource in ... pro20.php on line 28,

qadenza
  • 9,025
  • 18
  • 73
  • 126

1 Answers1

-2

There is nothing wrong with the code. The issue is likely that one (or both of the images) aren't being loaded correctly:

$stamp = imagecreatefrompng('img/wmark.png');
$im = imagecreatefromjpeg('img/test.jpg');

Check that img/wmark.png and img/test.jpg exist and are valid images.

I've tested your code (with some slight modification):

<?php

$stamp = imagecreatefrompng('wmark.png');
$im = imagecreatefromjpeg('img.jpg');

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

echo imagepng($im);

And I got this:

enter image description here

Nothing wrong with your code. The likely issue is that your images may not be valid png / jpg images. Please verify this.

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60