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
Related
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);
?>
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
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);
I was unable to watermark my images. I used the php manual found at this link
http://www.php.net/manual/en/image.examples-watermark.php
And also tried the sitepoint tutorial here
http://www.sitepoint.com/watermark-images-php/
but getting the same error that the image cannot be displayed because its have some errors. can somebody let me know whats wrong with the code or suggest me some better solution.
My code is here :
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('wm.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('img.jpg');
$size = getimagesize('img.jpg');
$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);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
Check all the return values of the functions you call, e.g.
$watermark = imagecreatefrompng('wm.png');
if ( !$watermark ) {
die('Sorry ' . __LINE__); // you might want to use something else here - just an example....
}
and then set the content type to image/jpeg not before you're actually trying to send the image
if ( headers_sent($file, $line) ) {
die('oops '.__LINE__);
}
else {
header('content-type: image/jpeg');
imagejpeg($image);
}
...makes it easier to pin-point the error.
I am using an image watermark code that works great but I also need to add a text watermark to it.
Here is the code in full:
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
You can use imagestring() there are other image text function so checkout the manual for GD
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);