PHP: imagePNG () will not save file to directory. File permissions are all correct, still not working

I am writing a program where watermarks are uploaded by users. After the layering is complete, imagePNG () outputs the photo to the browser, but does not save it to the directory. The paths are all correct and the file permissions have been changed to 0755. Using only the first function parameter (imagePNG ($ image)), the image is displayed, however, when the path is added (imagePNG ($ image, "photo_uploads /". $ Album_name. "/ ")).


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);


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


source to share

1 answer

I don't think your path is the correct directory. The reason is that it has to either save the file or output it to the browser, not both. If the filename is NULL, it will be output to the browser. Try using the fully qualified pathname.

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

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


This is more likely to work:

imagePNG($im, $path);


Use the full path:



This is how I do it:

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


Then for browser output (scaled):

  $base64 = base64_encode($png);
  echo "<img  width=\"$newWidth\" height=\"$newHeight\" src=\"data:image/png;base64,$base64\"  alt =\"profile photo\"/>";




All Articles