imagecopymerge() For two images Already in Server (watermark usage) - php

I have two photos already in server, trying to use one for WATERMARK..
I am using the below script:
<?php
$watermark = "../images/watermark/watermark.gif";
$image = "../images/mainphoto.gif";
$padding = 0;
$opacity = 100;
$watermark_size = getimagesize($watermark);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($image);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
?>
I am getting the following error:
Warning: imagecopymerge() expects parameter 1 to be resource, string given in /home/neatbuz/public_html/asite_service/inc_watermark.php on line 26
please help..

$image = imagecreatefromgif ( "../images/mainphoto.gif" );
$watermark = imagecreatefromgif ( "../images/watermark/watermark.gif" );
i'd recommend to use png instead of gif images

Related

imagecreatefromjpeg($file_name): failed to open stream

Hello everyone I am trying to add watermark to the uploaded images in WordPressbut when I use imagecreatefromjpeg($file_name) it throws this warning
Warning: imagecreatefromjpeg('file_name'): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized.
Here is the code I am using.
add_action('woocommerce_before_single_product_summary', 'adding_watermark', 20);
function adding_watermark() {
global $product;
$id = get_the_ID();
$name = get_the_title();
$link = get_the_permalink();
$padding = 3;
$opacity = 100;
$image = imagecreatefromjpeg($link);
echo '<pre>';
var_dump($image);
exit;
$watermark = imagestring($image, 1, 5, 5, 'TEST', '#fff');
$image_size = getimagesize($image);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
imagecopymerge($name, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
That var_dump() thing in the code always return bool(flase)
Can anyone give any hint why it is behaving so. Thank you in advacne.
Using the below code you can add watermark for your images,
//path to destination image
$destination_image = imagecreatefromjpeg('PATH/TO/DESTINATION/JPEG/FILE');
//path to watermark image
$watermark = imagecreatefrompng('PATH/TO/WATERMARK/PNG/FILE');
//calculate center position of watermark image
$watermark_left = (DST_IMAGE_WIDTH/2)-(WATERMARK_WIDTH/2); //watermark left
$watermark_bottom = (DST_IMAGE_HEIGHT/2)-(WATERMARK_HEIGHT/2); //watermark bottom
//use PHP imagecopy() to merge two images.
imagecopy($destination_image, $watermark, $watermark_left, $watermark_bottom, 0, 0, WATERMARK_WIDTH, WATERMARK_HEIGHT); //merge image
Source - https://www.sanwebe.com/2014/08/watermark-an-uploaded-image-with-php

PHP image script not working

i have script for gallery. He takes image and shows it on that image url. But its not working, images wont show up on gallery pages. Everything else works. Database is okay, so are files on FTP. Any idea? Here is code of that scrip (config.php is okay too)
<?
require ('config.php');
$img = $_GET['img'];
if (!isset($img) or (!is_numeric($img)))
{ $masterURL = 'images/no_image.jpg'; }
else
{
$fotosql = MySQL_Query ("SELECT photo FROM photo WHERE id = '$img' LIMIT
1");
$foto_file = MySQL_Result ($fotosql, 0, 'photo');
$masterURL = 'photos/$foto_file';
}
header('content-type: image/png');
$watermark = imagecreatefrompng('images/watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($masterURL);
$size = getimagesize($masterURL);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
$watermark_width, $watermark_height, 100);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>

PHP-GD Transparency of watermark PNG not correctly merged with JPEG

I am trying to install a watermark in the middle of my image, but every time it shows a weird square which is not fully transparent. This is the result of my code:
This is my code:
<?php
header("Content-type: image/png");
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
imagesavealpha($watermark,true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = (imagesx($image) - $watermark_width)/2;
$dest_y = (imagesy($image) - $watermark_height)/2;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
I usually create a new true colour resource and copy everything into it. This ensures GD doesn't get too quirky. It's a little bit more resource intensive, but should be negligible for most cases.
Below is your code modified to create a new image, copy in the jpeg, and then overlay the partially transparent watermark:
<?php
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$img_w = imagesx($image);
$img_h = imagesy($image);
$new = imagecreatetruecolor($img_w, $img_h);
imagecopy($new, $image, 0, 0, 0, 0, $img_w, $img_h);
imagedestroy($image);
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($img_w - $watermark_width) / 2;
$dest_y = ($img_h - $watermark_height) / 2;
imagecopy($new, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
header('Content-type: image/png');
imagejpeg($new);
imagedestroy($new);
imagedestroy($watermark);
Result:

background of watermark is black on zubrag watermark Script

i am applying watermark onto the image using this code why the background of the watermark image is black after applying watermark even the watermark is in png format and it is transparent this is the script zubrag
$image_path = $filename;
// Where to save watermarked image
$imgdestpath = $destination_folder . basename($filename);
// Watermark image
$img = new Zubrag_watermark($image_path);
$img->ApplyWatermark($watermark_path);
$img->SaveAsFile($imgdestpath);
$img->Free();
function ApplyWatermark($watermark_path) {
$this->watermark_path = $watermark_path;
// Determine image size and type
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];
$image_type = $size[2]; // 1 = GIF, 2 = JPG, 3 = PNG
// load source image
$image = $this->ImageCreateFromType($image_type, $this->image_path);
// Determine watermark size and type
$wsize = getimagesize($watermark_path);
$watermark_x = $wsize[0];
$watermark_y = $wsize[1];
$watermark_type = $wsize[2]; // 1 = GIF, 2 = JPG, 3 = PNG
// load watermark
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path);
// where do we put watermark on the image?
$dest_x = $size_x - $watermark_x - $this->offset_x;
$dest_y = $size_y - $watermark_y - $this->offset_y;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, 100);
$this->image = &$image;
$this->watermark = &$watermark;
$this->image_type = $image_type;
}
Try this - change
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, 100);
to use a slightly different function, and remove the final variable:
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y);

PHP: Watermark won't work

I've created a watermark function as below :
index.php
<?php
include 'core.inc.php';
do_watermark('bg1.jpg', 'logo1.png');
?>
core.inc.php
<?php
header ( 'Content-type: image/jpeg' );
function do_watermark($source_image, $logo) {
$watermark = imagecreatefrompng ( $logo );
$watermark_width = imagesx ( $logo );
$watermark_height = imagesy ( $logo );
$image = imagecreatetruecolor ( $watermark_width, $watermark_height );
$image = imagecreatefromjpeg ( $source_image );
$image_size = getimagesize ( $source_image );
$x = $image_size [0] - $watermark_width - 10;
$y = $image_size [1] - $watermark_height - 10;
imagecopymerge ( $image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );
imagejpeg ( $image );
}
?>
But when calling do_watermark('bg1.jpg', 'logo1.png'), show nothing.
bg1.jpg and logo1.png is along index.php.
Any help would be great.
Isn't the watermark image usually going to be smaller than the image being watermarked? You have created your working image to be the watermark's size rather than the source image's size -- is that what you really want? I'm not sure about the x and y coordinates -- the larger the watermark is, the further right and (up or down?) it will be, as x and y increase with the size of the watermark. I would think about positioning it (x and y) as a function of both the image size and the watermark size.
You also have it hard coded for JPEG image and PNG watermark. You could get the image types from the file names or from the getimagesize() call ('mime' entry). Consider making it more flexible this way.
There were some major problems in the code you had written, this should work, but compare it to yours to see what was wrong.
<?php
function do_watermark($source_image, $logo) {
$watermark = imagecreatefrompng($logo);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatefromjpeg($source_image);
$image_width = imagesx($image);
$image_height = imagesy($image);
$x = $image_width - $watermark_width - 10;
$y = $image_height - $watermark_height - 10;
imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
?>

Categories