change opacity of image using imagick - php

I want to change opacity of multiple images when i used setImageOpacity it work fine for all images but not with png images and when i used evaluateImage it is work fine for transparent images but not for other images. how can i used same method for all types of images if image is transparent or not this is the code
<?php
// Open the original image
$image = new Imagick();
$image->readImage(3.jpg);
// Open the watermark
$watermark = new Imagick();
$watermark->readImage(2.png);
$watermark->setImageOpacity(0.444);
//$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.0, Imagick::CHANNEL_ALPHA);
$watermark->rotateImage(new ImagickPixel('transparent'), 90);
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 20, 20);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image;

use if condition using getImageAlphaChannel() function
to detect if the image have any transparent
note :
This method is available if Imagick has been compiled against
ImageMagick version 6.4.0 or newer.

Related

PHP - convert EPS to PNG using Imagick

I am trying to convert eps to png image using imagick.This is the code i am using.
$path = getcwd().'/uploads/1488/791/586/imprint_option_1A.eps';
$save_path = getcwd().'/uploads/1488/791/586/imprint_option_2E_c.png';
$image = new Imagick();
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->setResolution(300,300);
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);
but the transparency is not working i got image with white background ( Result image ). and when we scale image it loses clarity..
Any idea ?
Here is my eps file https://drive.google.com/open?id=0Bwq4DvGGbHVfT0FYTE94WW5GTnc
The function setResolution should be called before reading the image. Thus
$image = new Imagick();
$image->setResolution(1200, 1200);
$image->readImage($path);
should do it. As for the transparency, can you try to get the input as sRGB instead of CMYK? If I convert first the input file to pdf with epstopdf and then use this converted file in the PHP script, it produces a transparent PNG file.

PHP GD - imagerotate for a jpeg image is not working

Title says JPEG. But I tried PNG. It didn't work.
GD supports imagerotate function.
if (function_exists('imagerotate')) {
echo "test";
}
It outputs the word test. So i assume I have imagerotate function.
$im = imagecreatetruecolor($width + 10, $height + 10);
...
I did some image porcess. I can see the processed image without any problem. But i want to rotate the final image. So i did the following.
imagerotate($im,180,0);
imagepng($im,$png,9);
imagedestroy($im);
But I am still getting the image without rotation.
I even just tried to rotate a image without doing any process. It didn't work too.
You need to assign the rotated image to another variable before create the png.
$rotatedImage = imagerotate($im,180,0);
imagepng($rotatedImage,$png,9);
imagedestroy($rotatedImage);

PHP image watermark only displaying image on page

I am testing a script where I watermark an image in my webpage.
The script works fine and the image is watermark but my problem is that only the image is displayed on the page.
As soon as I add the script to my page it's like the web page is converted to the image that I'm watermarking.
I think it's because of header("content-type: image/jpeg"); from the code.
I need to watermark the image on my webpage but I also need the rest of my webpage to be displayed too.
How is this done? I'm quite confused on how this works.
The script I'm using is from here
Here's the code I'm using:
<?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);
?>
NOTE: I'm getting the image path from the database so I cannot hardcode the image filename as it's dynamic.
This is a bit mind-boggling if you're doing it for the first time, but once you see how it works it's really simple. ;)
You need one script that generates the image (e.g. image.php) and then your main script references that image (e.g. <img src="image.php">). It's not possible to have one request/script return both a document and an image simultaneously.
(PS. it's actually possible but it involves encoding the image as a very weird kind of src attribute. You don't want to do it, trust me.)
I believe you got confused about how this works: The watermark generating code above is not meant to be included in the page generating scripts/logic. Instead, it needs to be accessible behind it's own URL and then linked via <img src="./path/to/script"/> into the site you want to contain that image.
Put this script entirely separated from your "main website" script. On your website, use an <img> tag, with that watermarking script as the source.
You can't combine two different content-types like you're appearing to do. If you want a "web page" to appear, the content-type needs to be text/html. If you want just an image to appear, the content-type needs to be image/*.

Resizing image with PHP

I used a very simple code to resize image with PHP; but surprisingly it does not work for some images. The problem should be associated with imagecreatefromjpeg(), as it will generate a black image (which is of the background image).
$picture="test5.jpg";
$url="http://www.pokerpurist.com/uploadedImages/bettingpro/NewsImages/TN98553_Perla-Beltran.jpg";
list($width, $height) = getimagesize($url);
$new_height = $height / $width * 400;
$image_p = imagecreatetruecolor(400, $new_height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height);
imagejpeg($image_p, $picture);
echo "<img src='$picture' />";
This problem happens offen, and I included an example image. What is the problem with these images leading to this problem? It seems to be a normal JPG image.
By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Your example image is a PNG, not JPEG. You probably need to put some detection code in place...
Edit: exif-imagetype or ImageMagick might be of some use.
#By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Use Asido: PHP Image Processing Solution
Asido supports the following features:
pluggable drivers for GD2 (php_gd2), MagickWand (php_magickwand),
ImageMagick extension (php_imagick) as well as ImageMagick shell
commands
"hack" drivers: workarounds for certain disabilities of a particular driver by using some of the other functionality provided
by the environment
various resize functionality: proportional resize, resize only by width or height, stretch resize, fit resize, frame resize
watermark images, including tiling watermark and automatic scaling of large watermarks
rotate images
copy images onto one another
crop images
grayscale images
convert images between different filetypes
If you can't access Asido website, you can download Asido from SourceForge.net

How to create watermark with imagemagick

I'm trying to create a watermark with ImageMagick however, the guides on layering are pretty daunting. Basically I'll have one base image say "base.jgp" and an overlay image say "overlay.jpg". Overlay.jpg would be smaller than base.jpg. Which exec command would I run to place overlay centered on top of base.jpg?
Thanks!
shell_exec("composite -gravity center ./images/watermark_horizontal.png {$this->path} {$this->path}");
Here we go
Check out ImageMagick examples, especially the Compositing Images chapter. It has a number of ready-made real-world examples.
$image = new Imagick();
$image->readImage("image.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("watermark.png");
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());

Categories