Im Trying to watermark and resize and imagen but im doing something wrong on the resize part any help please
$image = #imagecreatefromjpeg('15_pic1.jpg');
$watermark = #imagecreatefrompng('watermark.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth));
$startheight = (($imageheight - $watermarkheight));
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
//resize
list($width, $height) = getimagesize($image);
imagecopyresized($thumb, $image, 0, 0, 0, 0, 300, 300, $width, $height);
header("Content-type: image/jpeg"); imagejpeg($thum);
imagedestroy($image);
imagedestroy($watermark);
Got it working here is the code
$img = "15_pic1.jpg";
$wat = "watermark.png" ;
$wit = "300";
$hei = "300";
$imagestart = #imagecreatefromjpeg($img);
$watermark = #imagecreatefrompng($wat);
list($width, $height) = getimagesize($img);
$imagewidth = imagesx($imagestart);
$imageheight = imagesy($imagestart);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth));
$startheight = (($imageheight - $watermarkheight));
imagecopy($imagestart, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
$thumb = imagecreatetruecolor($wit, $hei);
imagecopyresized($thumb, $imagestart, 0, 0, 0, 0, $wit, $hei, $width, $height);
header("Content-type: image/jpeg"); imagejpeg($thumb);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($thumb);
Related
so my following code is working like if i upload image it will resize image to 720x450 and then watermark it. but i wish not to modify the width and height and put the watermark at the bottom right of an image of any size
if someone could help me out here?
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 720; $height = 450;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
appreciate your help and time.
You are providing manual height and width, Just assign the original height ans width of image
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth; $height = $oheight;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
Try this will work as you expected.
for more info check here http://php.net/manual/en/image.examples-watermark.php
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:
Adding water mark to jpg/jpeg and png works perfectly file but strange color appear when the same watermark is added to the gif image
the gif image with watermark is here
the strange color here is my png watermark image which is shown like this
my code
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
Solved this after converting the gif image to a true color image
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
$tmp = imagecreatetruecolor(imagesx($image), imagesy($image));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$image = $tmp;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
I have a problem of image quality after a resize. Could you help me to find a solution to my problem?
This is a page where an image has been resized
goo.gl/5mYxxb , 460*275 and this the code that I am using to resize :
$newwidth = 460;
$newheight = 275;
$newwidth1 = 146;
$newheight2 = 88;
$temp = imagecreatetruecolor($newwidth, $newheight);
$black2 = imagecolorallocatealpha($temp,0,0,0,65);
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg($path);
imagecopyresampled($temp, $src, 0,0,0,0, $newwidth, $newheight, $width, $height);
Imagettftext($temp, 16, 0, $start_x, $start_y, $black2, $font , "text");
imagejpeg($temp, $path,95);
imagedestroy($temp);
$temp2 = imagecreatetruecolor($newwidth1, $newheight2);
imagecopyresampled($temp2, $src, 0,0,0,0, $newwidth1, $newheight2, $width, $height);
Imagettftext($temp2, 8, 0, $start_x2, $start_y2, $black2, $font , "text");
imagejpeg($temp2,$pathFile ,90);
imagedestroy($temp2);
imagedestroy($src);
I have a script from an old website called FancyResize, that resizes images to a new requested width and height. For some reason it now "destroys" images when used - not every time, but in many circumstances it makes a weird color overlay, changes pixels etc., so it is impossible to see what image it actually should have been. I guess it could have something to do with new PHP version etc., but I don't know, but I guess it have worked before.
So, my question is: can anybody see a mistake in this?
<?
$new_width = $_REQUEST[w];
$new_height = $_REQUEST[h];
$file = $_REQUEST[i];
$localpath = "WEBSITENAME";
$query = $_SERVER[QUERY_STRING];
$datafile = $_SERVER[DOCUMENT_ROOT].'/img/fronttemp/'.md5($query);
if (file_exists($datafile))
{
header("Content-type: image/jpeg");
die(file_get_contents($datafile));
}
if (substr($file, 0, strlen($localpath)) == $localpath)
{
$file = substr($file, strlen($localpath));
$file = $_SERVER[DOCUMENT_ROOT].$file;
}
else
{
$file = substr($query, strpos($query, "&i=")+3);
}
$image = imagecreatefromjpeg($file);
if (!$image)
{
$image =imagecreatefromgif($file);
$is_gif = true;
}
else $is_gif = false;
if (!$image) die("error reading gif/jpg $file");
$info = getimagesize($file) or die("error getting info");
$width = $info[0];
$height = $info[1];
$input_landscape = ($width/$height)>1;
$output_landscape = ($new_width/$new_height)>1;
function imageScaleToWidth($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
return $scaledimage;
}
function imageScaleToHeight($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$scale_width = ceil(($new_height/$height)*$width);
$scaledimage = imagecreatetruecolor($scale_width, $new_height);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
return $scaledimage;
}
function makeImageWideEnough($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
if ($width < $new_width)
{
$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
return $scaledimage;
}
return $image;
}
function makeImageHighEnough($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
if ($height < $new_height)
{
$scale_width = ceil(($new_height/$height)*$width);
$scaledimage = imagecreatetruecolor($scale_width, $new_height);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
return $scaledimage;
}
return $image;
}
function imageCrop($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$croppedimage = imagecreatetruecolor($new_width, $new_height);
$sx = ($width-$new_width)/4;
$sy = ($height-$new_height)/4;
imagecopy($croppedimage, $image, 0, 0, $sx, $sy, $new_width, $new_height);
return $croppedimage;
}
function scaleWidth($image, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$ratio = $new_width / $width;
$sx = ceil($width*$ratio);
$sy = ceil($height*$ratio);
$scaledimage = imagecreatetruecolor($sx, $sy);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $sx, $sy, $width, $height);
return $scaledimage;
}
$croppedimage = scaleWidth($image, $new_width);
if ($new_height < imagesy($croppedimage))
{
$croppedimage = imageCrop($croppedimage,$new_height,$new_width);
}
header("Content-type: image/jpeg");
imagejpeg($croppedimage, $datafile);
echo file_get_contents($datafile);
?>