I want to use watermark image with php. This is my reference page
function imagecreatefromfile($image_path)
{
list($width, $height, $image_type) = getimagesize($image_path);
switch ($image_type)
{
case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
default: return ''; break;
}
}
$image = imagecreatefromfile($_GET['image']);
if (!$image) die('Unable to open image');
$watermark = imagecreatefromfile('uploads/files/water.png');
if (!$image) die('Unable to open watermark');
$watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
$watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0,
imagesx($watermark), imagesy($watermark));
header('Content-Type: image/jpg');
imagejpeg($image, '', 100);
imagedestroy($image);
imagedestroy($watermark);`
but my page seems empty ,
This is my test page
http://www.alanyaticaretrehberi.com/watermark.php?image=uploads/firmaresim/750/address-cikcilli-3jpg.jpeg
this worked in other servers but not worked in my server.
This is my phpinfo page..
http://www.alanyaticaretrehberi.com/php.php
I guess some setting missed in my php settings but i dont know what it is. May be it is about gd library or something else, can you give some advices for this issue.
Related
I'm building a wallpaper website and therefore i need to be able to resize the original image before downloading. I tried this code to resize the image:
//resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
$imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
switch($mime){
case 'image/gif':
$image_create = "imagecreatefromgif";
$image = "imagegif";
break;
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 7;
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 80;
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
//if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
if($width_new > $width){
//cut point by height
$h_point = (($height - $height_new) / 2);
//copy image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
}else{
//cut point by width
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
And this will do the resize:
resize_crop_image($width, $height, $image_URL, $save_URL)
This code works fine for me but i want to ONLY send the output to user's browser, since saving thousands of extra images isn't possible. There are libraries i can use, but i don't want to use a third party snippet. Is there a way to alter this code in the way i desire?
Thanks.
For your $image function , do not specify a destination directory (null) and an image stream will be created.
header("Content-type:{$mime}");
That should send the image to the user
You just need to set the proper headers and echo the output. Every PHP request "downloads a file" to the browser, more or less. You just need to specify how the browser handles it. So try something like:
header("Content-Type: $mime");
header("Content-Disposition: attachment; filename=$dst_img");
echo $dst_img;
That should do it for you.
I've got a GD Library upload script which processing and resizes images. After uploading PNGs are getting rendering as a different image, some stock-looking image, on the browser side, yet when opened on disk they are correct. I've re-saved the affected pngs via photoshop and uploaded directly, this fixes the display issue, but when i upload that image via the script the display issue returns, thus the script encoding seems to cause issue with the browser display, but why i know not.
Hosting environment is Hetzner Server.
Upload Sample:
//resize images
protected function resize($img, $w, $h, $newfilename) {
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get Image size info
$imgInfo = getimagesize($img);
switch ($imgInfo[2]) {
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
//If image dimension is smaller, do not resize
if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
$nHeight = $imgInfo[1];
$nWidth = $imgInfo[0];
}
else{
// yeah, resize it, but keep it proportional
if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
$nHeight = $h;
}
else{
$nWidth = $w;
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
}
}
$nWidth = round($nWidth);
$nHeight = round($nHeight);
$newImg = imagecreatetruecolor($nWidth, $nHeight);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
//Generate the file, and rename it to $newfilename
switch ($imgInfo[2]) {
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename,100); break;
case 3: imagepng($newImg,$newfilename,0); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
return $newfilename;
}
Can anyone give me some insight into this. Will provide more info if necessary. Ty
EDIT:
Browser Display
Disk Display
I have no clue where that image is coming from, it's on multiple uploads, when browsing the full path to the image on the browser the same image comes up so it's not a pathing issue.
I have implemented the following function:
private static function generatePicture($sizeKey, $src, $initialWidth, $initialHeight, $imageType) {
$destination = AdImage::addSizeKey($sizeKey, $src);
if (file_exists($destination)) {
return;
}
$finalSize = AdImage::$sizes[$sizeKey];
$initialRatio = $initialWidth / $initialHeight;
$finalRatio = $finalSize["w"] / $finalSize["h"];
$newImage = imagecreatetruecolor($finalSize["w"], $finalSize["h"]);
imagealphablending( $newImage, false );
imagesavealpha( $newImage, true );
$oldImage = imagecreatefromstring(file_get_contents($src));
if ($initialRatio >= $finalRatio) {
$scale = $finalSize["h"] / $initialHeight;
$surpluss = abs($initialWidth - ($initialHeight * $finalRatio));
imagecopyresized($newImage, $oldImage, 0, 0, $surpluss / 2, 0, $finalSize["w"], $finalSize["h"], $initialWidth - $surpluss, $initialHeight);
} else {
$scale = $finalSize["w"] / $initialWidth;
$surpluss = abs($initialHeight - ($initialWidth / $finalRatio));
imagecopyresized($newImage, $oldImage, 0, 0, 0, $surpluss / 2, $finalSize["w"], $finalSize["h"], $initialWidth, $initialHeight - $surpluss);
}
switch ($imageType) {
case IMAGETYPE_JPEG: {
imagejpeg($newImage, $destination, 100);
break;
}
case IMAGETYPE_PNG: {
imagepng($newImage, $destination);
break;
}
case IMAGETYPE_GIF: {
imagegif($newImage, $destination);
break;
}
default:
{
imagejpeg($newImage, $destination, 100);
break;
}
}
}
However, edges appear, lines are not straight, even if they were originally straight. I believe that I can solve my problem with a combination of high-pass filter, low-pass filter and salt & piper filter, but I am not sure how I implement it for png, jpeg and gif. I would start by studying what is the pattern of these files and apply a filter separately. I wonder if there is a better solution.
I did not need image processing. I was using the wrong function.
Instead of
imagecopyresized
I use now
imagecopyresampled
and the quality of the images has improved.
I have tried many solutions and the truth and got to the point where you do not know what else to do.
The following image is a PNG ("cover.png"):
As you will have a blank oval really is completely transparent. With PHP I'm trying to fuse it to this picture ("lapiz.jpg"):
However, despite how much I've tried to not get the clear space of the first image is transparent and instead goes completely blank, covering the image that should melt.
For now this is my code:
$img_user = 'fotos/lapiz.jpg';
$img_user_type = getImageInfo($img_user,'type');
$posX = 404;
$posY = 2;
$width = getImageInfo($img_user,'width');
$height = getImageInfo($img_user,'height');
$stamp = 'fotos/cover.png';
switch($img_user_type)
{
case 'jpeg':
$img_user_create = imagecreatefromjpeg($img_user);
break;
case 'gif':
$img_user_create = imagecreatefromgif($img_user);
break;
case 'png':
$img_user_create = imagecreatefrompng($img_user);
break;
}
$im = imagecreatefrompng($stamp);
imagealphablending($im, false);
imagesavealpha($im, true);
imagecolortransparent($im, imagecolorallocate($im, 255, 255, 255));
imagecopymerge($img_user_create, $im, $posX, $posY, 0, 0, $width, $height, 100);
header('Content-Type: image/png');
imagepng($im);
ImageDestroy($im);
ImageDestroy($img_user_create);
What I can be doing wrong?
junihh resolved this using the imagemagick library and the code below:
$img1 = new Imagick('fotos/lapiz.jpg');
$img2 = new Imagick('fotos/cover.png');
$posX = 404;
$posY = 2;
$img2->compositeImage( $img1, imagick::COMPOSITE_DSTOVER, $posX, $posY );
header('Content-type: image/png');
echo($img2);
How do you download, resize and store an image from a remote server using php?
This is the code I am using
$temp_image = file_get_contents($url);
$image = imagecreatefromstring($temp_image);
$thumb = imageToCanvas($image,100,75,true);
imagejpeg($thumb,$base_image_path . $thumb_path,90)
function imageToCanvas($_image, $_canvasWidth, $_canvasHeight, $forceScale=false,$x=false,$y=false)
{
$newImage = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
$imageinfo = getimagesize($_image);
$sourceWidth = $imageinfo[0];
$sourceHeight = $imageinfo[1];
$sourceImage = openImage($_image);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $_canvasWidth, $_canvasHeight, $sourceWidth, $sourceHeight);
return $newImage;
}
function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));
switch($extension) {
case '.jpg': case '.jpeg':
$img = #imagecreatefromjpeg($file);
break;
case '.gif':
$img = #imagecreatefromgif($file);
break;
case '.png':
$img = #imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
Doesn't work and I don't know why.
$sourceWidth & $sourceHeight doesn't have a value so I presume $image is in the wrong format
Thanks!
There is no function in php called openImage so that would be a problem if you don't define it yourself.
If you do have it defined, what does it look like and are you receiving any errors?
Edit: Based on your comments the problem would seem to be that you treat the input parameter of your openImage function as a file path. However, when you call it you are feeding it an image resource, the result of imagecreatefromstring.
If you are on a linux server and ghostscript is installed - here is an easier way
shell_exec("convert in.jpg -resize 100x75 out.jpg")