简体   繁体   中英

php watermark and save image

I am trying to add a watermark to an image and then save the image to another file with php. This is the code that I have so far, but for some reason the watermark is not appearing on the image in the new directory. The original image is saved in the path $old_path and I want to save it to $new_path after applying the watermark.

    $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

Please tell me what I am doing wrong.

您永远不会将imagecopy结果写入任何文件,只需将旧图像重命名为新路径 - 请改用imagejpeg

 imagejpeg($logoImage, $new_path);

You aren't outputting the image anywhere. You need to output it. All you're doing right now is renaming your old file to the new.

Try imagepng() or equivalent for your format. http://php.net/manual/en/function.imagepng.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM