duadpnld426905 2015-05-03 03:42
浏览 1021

PHP:imagePNG()不会将文件保存到目录。 文件权限全部正确,仍然无法正常工作

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.

  • 写回答

1条回答 默认 最新

  • douyan1613 2015-05-03 04:25
    关注

    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\"/>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建