3

I am writing a program that watermarks user uploaded photos. After completing the layering, imagePNG() outputs the photo to the browser but wont save it to a directory. The paths are all correct and file permissions altered to 0755. Using only the first parameter of the function ( imagePNG($image) ) the image outputs, however when the path is added ( imagePNG($image, "photo_uploads/" . $album_name . "/") ).

Code:

 <?php
session_start();
use PHPImageWorkshop\ImageWorkshop;
$album_length = $_SESSION['album_length'];
$extension_array = $_SESSION['extension_array'];
$album_name = $_SESSION['album_name'];
chmod("photo_uploads/" . $album_name . "/", 0777);
for($i = 0; $i < $album_length; $i++) {
    $path = 'photo_uploads/' . $album_name . '/' . $i .     $extension_array[$i];
    // Load the stamp and the photo to apply the watermark to
    $stamp = imagecreatefrompng('watermark.png');
    //only works with png
    $im = imagecreatefrompng($path);

    // Set the margins for the stamp and get the height/width of the stamp image
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    // Copy the stamp image onto our photo using the margin offsets and the photo 
    // width to calculate positioning of the stamp. 
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

    // Output and free memory
    header('Content-type: image/png');
    $save = "photo_uploads/" . $album_name . "/";
    imagePNG($im, $save);
    imagedestroy($im);
}
?>

I have tried all of the solutions for other similar question. The function continues to output the modified image unless a path to save to is added as a second parameter.

nick_pitoniak
  • 31
  • 1
  • 2

1 Answers1

2

I do not think your path is correct the directory. The reason being it should either save the file or output to the Browser, not both. If the filename is NULL then it will output to the Browser. Try using the full path name.

File names do not end with a slash
This is not correct:

$save = "photo_uploads/" . $album_name . "/";

This has more chance of working:

imagePNG($im, $path);

Use the full path:

/home/user/public_html/photo_uploads/something.png

This is how I do it:

  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $fp = fopen($filename  ,"w");
  fwrite($fp, $png);
  fclose($fp);

Then to output to Browser (scaled):

  $base64 = base64_encode($png);
  echo "<img  width=\"$newWidth\" height=\"$newHeight\" src=\"data:image/png;base64,$base64\"  alt =\"profile photo\"/>";
Misunderstood
  • 5,534
  • 1
  • 18
  • 25