php水印和保存图像


php watermark and save image

我正在尝试为图像添加水印,然后用php将图像保存到另一个文件中。这是我到目前为止的代码,但由于某种原因,水印没有出现在新目录中的图像上。原始图像保存在路径$old_path中,我想在应用水印后将其保存到$new_path

    $old_path = "images_upload/".$name.".".$type;
    $new_path = "images_main/".$name.".".$type;
    ////////////////////water mark
$main_image = imagecreatefromstring(file_get_contents($old_path));
// Load the logo image
$logoImage = imagecreatefrompng("assets/watermark.png");
imagealphablending($logoImage, true);
// Get dimensions
$imageWidth=imagesx($main_image);
$imageHeight=imagesy($main_image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage); 
// Paste the logo
imagecopy(
   // source
   $main_image,
   // destination
   $logoImage,
   // destination x and y
   $imageWidth-$logoWidth, $imageHeight-$logoHeight,
   // source x and y
   0, 0,
   // width and height of the area of the source to copy
   $logoWidth, $logoHeight);
    ////////////////////////
    rename($old_path, $new_path);// save image

请告诉我我做错了什么。

您从不将imagecopy结果写入任何文件,只需将旧图像重命名为新路径-使用imagejpeg代替:

 imagejpeg($logoImage, $new_path);

您不会在任何地方输出图像。你需要输出它。你现在所做的就是将旧文件重命名为新文件。

请尝试使用imagepng()或等效格式。http://php.net/manual/en/function.imagepng.php