ImageMagick move watermark from top left to bottom right? - php

I've got this working adding a watermark onto my image at the top left, How can i change it so it adds in the bottom right?
Here is my current code:
$watermark = new Imagick();
$watermark->readImage("images/watermark_boxart.png");
$im->compositeImage($watermark, imagick::COMPOSITE_OVER, 3, 3);
Thanks

You're setting the watermark at (3,3). You need to set it in the bottom right by placing these numbers accordingly.
Methodology:
Calculate width and height on $im [$im_height, $im_width]
Calculate width and height of $watermerk [$wm_height, $wm_width]
Find the coordinates to place the watermark at via calculation of differences in dimensions
Create composite with calculated locations
Code:
$im_d = $im->getImageGeometry();
$im_w = $im_d['width'];
$im_h = $im_d['height'];
$watermark = new Imagick();
$watermark->readImage("images/watermark_boxart.png");
$watermark_d = $watermark->getImageGeometry();
$watermark_w = $watermark_d['width'];
$watermark_h = $watermark_d['height'];
$margin = 3;
$x_loc = $im_w - $watermark_w - $margin;
$y_loc = $im_h - $watermark_h - $margin;
$im->compositeImage($watermark, imagick::COMPOSITE_OVER, $x_loc, $y_loc);

Related

PHP imagerotate is cropping image

I am writing a script that takes an arrow image and rotates it by a set number of degrees. Using the code below, when the angle is a multiple of 90 the image rotates and displays as expected.
The source image looks like this (74 x 74):
Images after rotating by 90:
Images after rotating by any other number (not a multiple of 90) eg 45:
As can be seen in the image, the tip of the arrow has been cropped out of the image. Could anyone please tell me why this is happening? Again, multiples of 90 are fine, it's just any other number where the unusual cropping occurs.
$props = ['w' => 74, 'h' => 74];
$angle = 360 - $_GET['angle'];
$final_img = imagecreatetruecolor($props['w'], $props['h']);
imagesavealpha($final_img, true);
$transColor = imagecolorallocatealpha($final_img, 0, 0, 0, 127);
imagefill($final_img, 0, 0, $transColor);
$rotate = imagecreatefrompng('arrow.png');
$src = imagerotate($rotate, $angle, $transColor); //rotated my image
$src_x = ImageSX($src); //find out new x width
$src_y = ImageSY($src); //find out new y height
$src_widthx = $src_x/2 - $props['w']/2; // divide each by 2 and then subtract desired end width from wider rotated width
$src_heighty = $src_y/2 - $props['h']/2; // and again for height
imagecopy($final_img, $src, 0, 0, $src_widthx, $src_heighty, $props['w'], $props['h']);
header('Content-Type: image/png');
imagepng($final_img);
When you rotate a square of nXm pixels by lets say 45 degrees you will get the diagonals(which are bigger than n or m and equal sqrt(n^2+m^2)) of the image be the new rotated image width and height.
The function crops the rotated image using the original dimensions of the image, namely n and m.
A way to fix the problem would be by crating a bigger blank image with the appropriate size, sqrt(width_original_image^2+height_original_image^2), and than copy the original image to the new image using imagecopy. After that you can use imagerotate on the new image
I installed and used the ImageMagick PHP library and the rotations show uncropped, no matter the degree of rotation.

php imagemagick - how to square an image in the middle of a white square without cropping

I have a series of images on white backgrounds.
My problem is they are in a variety of shapes and sizes and I want them to all be equal in size and all centred in a square ratio without cropping and losing any of the actual image.
Below is my best attempt to date (using imagemagik), but the is not scaling it is just cropping square at 80x80 and losing most of the content
$im = new Imagick("myimg.jpg");
$im->trimImage(20000);
$im_props = $im->getImageGeometry();
$width = $im_props['width'];
$height = $im_props['height'];
$diff = abs($width-$height);
$color=new ImagickPixel();
$color->setColor("white");
if($width > $height){
$im->thumbnailImage(80, 0);
$im->borderImage($color, ($diff/2), 0);
}else{
$im->thumbnailImage(0, 80);
$im->borderImage($color, 0, ($diff/2));
}
$im->cropImage (80,80,0,0);
$im->writeImage("altimg.jpg");
Any help gratefully recieved
Thanks #Mark Setchel for pointing me in the right direction. I managed to achieve what I wanted, (an un-cropped image centred in a white square and trimmed to the longest side).
I have voted up your comments but thought I would post my final code for completeness.
$im = new Imagick("myimg.jpg");
$im->trimImage(20000);
$im->resizeImage(80, 80,Imagick::FILTER_LANCZOS,1, TRUE);
$im->setImageBackgroundColor("white");
$w = $im->getImageWidth();
$h = $im->getImageHeight();
$off_top=0;
$off_left=0;
if($w > $h){
$off_top = ((80-$h)/2) * -1;
}else{
$off_left = ((80-$w)/2) * -1;
}
$im->extentImage(80,80, $off_left, $off_top);
$im->writeImage("altimg.jpg");

Get width and height of scaled down image after imagerotation?

A followup to this question:
How to get new width and height after an image has been rotated with imagerotate()?
The answer I got is based on the filenames actual image size, but if I want to start from another width and height.
How would I achieve that? See below code for my attempt...
$ps['product_angle'] = 77; //Could be any angle
$filename = 'test.png' //filename to the original product
list($source_width, $source_height) = getimagesize($filename);
$source_image = imagecreatefromjpeg($filename);
//Example for clarification (this is parameters that is used to save new image)
$ps['product_width'] = 40; //for example $source_width = 200
$ps['product_height'] = 80; //and $source_height = 400
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {
//Actual dimensions of image from filename
$current_source_x = imagesx($source_image);
$current_source_y = imagesy($source_image);
//Get current ratio of "scaled down image"
$ratio_x = $ps['product_width'] / $current_source_x;
$ratio_y = $ps['product_height'] / $current_source_y;
$source_image = imagerotate(
$source_image,
360-$angle,
imageColorAllocateAlpha($source_image, 255, 255, 255, 127)
);
//New dimensions from actual filename
//This would be fine if I just wanted the new width and height
//based on the filenames dimension, but I want new dimensions
//for the "scaled downed version" of the image (40, 80)
$new_image_source_x = imagesx($source_image);
$new_image_source_y = imagesy($source_image);
//I tried this, but obviously I'm doing something totally wrong
$new_width = ($new_image_source_x - $current_source_x) * $ratio_x;
$new_height = ($new_image_source_y - $current_source_y) * $ratio_y;
//Set new width after rotation
$ps['product_width'] = $new_width;
$ps['product_height'] = $new_height;
}
$ps['source_image'] = $source_image;
list($source_width, $source_height) = getimagesize($filename);
$dest_width = (int)$ps['product_width'];
$dest_height = (int)$ps['product_height'];
//Resize source-image to new width and height
//
imagecopyresized($dest_image, $ps['source_image'], 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);
Probably I'm missing something very essential....
UPDATE
An example of real values...
image current width63.224619257754
image current height80.210337864315
//after calculation
image new width37.583523669887
image newt height21.716336015666
where angle is 41.10419020401479
Looking at my comment
"I've actually came into a solution"
.. It wasn't the day when I used my best english skills...
I guess you know what I meant though and here is how I solved my issue:
In my approach I was trying to calculate the new values based on ratio from the "scaled down image" and then get new width and height based on the difference from the "scaled down image" and "the image after rotated".
Something like this:
Set the relation/ratio between "scaled down image" and original image.
Do the actual image-rotation
Get the difference between original dimensions from the rotated image and multiply that with ratio-factor set before image was rotated.
Get the new width and height based on the rotation angle
This really didn't work correctly (failed at step4). I've been looking around a lot to find answers how to calculate width and height after an image is rotated. But these width and heights didn't return the same dimensions that the GD-functions imagesx() and imagesy() returned after the image was rotated. I've tried out several of calculations using sin() and cos() for retrieving the width and height but still didn't get exact same values as imagesx() and imagesy().
That got me thinking... What if I change my approach to:
Set the relation/ratio between "scaled down image" and original image.
Do the actual image-rotation
Apply the new dimensions based on the ratio multiplied with imagesx() and imagesy() - values return after the rotation
New code:
//Rotate resized image (if it should be)
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {
//Get current dimensions from file
$current_source_x = imagesx($source_image);
$current_source_y = imagesy($source_image);
//Get current ratio of "scaled down image"
$ratio_x = $ps['product_width'] / $current_source_x;
$ratio_y = $ps['product_height'] / $current_source_y;
//Rotate image
$source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));
//Now we get a new width from the imagerotate()-function, use those to set new_width from
//ratio/propoprtions is used from origin width and height
$ps['product_width'] = imagesx($source_image) * $ratio_x;
$ps['product_height'] = imagesy($source_image) * $ratio_y;
}
This worked fine - almost... the issue now was that the if the rotated image's new width and/or height got larger then the origin dimensions of the image, then the proportions wouldn't be accurate (and would cut off the imagesometimes (depending the rotation angle)).
The code was modified so heights and widths would be in proportion to the image from the file when creating the resized image.
//Rotate resized image (if it should be)
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {
//Get current dimensions from file
$current_source_x = imagesx($source_image);
$current_source_y = imagesy($source_image);
//Get current ratio of "scaled down image"
$ratio_x = $ps['product_width'] / $current_source_x;
$ratio_y = $ps['product_height'] / $current_source_y;
//Rotate image
$source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));
//Now we get a new width from the imagerotate()-function, use those to set new_width from
//ratio/propoprtions is used from origin width and height
$ps['product_width'] = imagesx($source_image) * $ratio_x;
$ps['product_height'] = imagesy($source_image) * $ratio_y;
//Set these so we can modifiy the width and height given from getimagesize()-function below
$ps['source_width'] = imagesx($source_image) ;
$ps['source_height'] = imagesy($source_image);
}
//If image is rotated, then width and height are adjusted with these values
if (isset($ps['source_width']) && isset($ps['source_height']) ) {
$source_width = $ps['source_width'];
$source_height = $ps['source_height'];
}
//Set position where to place in the image to save
$dest_x = $ps['product_left'];
$dest_y = $ps['product_top'];
$dest_width = (int)$ps['product_width'];
$dest_height = (int)$ps['product_height'];
//Resize source-image to new width and height and then copy from source to destination point
imagecopyresized($dest_image, $ps['source_image'], $dest_x, $dest_y, 0, 0, $dest_width, $dest_height, $source_width, $source_height);
So my final solution would include these steps:
Set the relation/ratio between "scaled down image" and original image.
Do the actual image-rotation
Apply the new dimensions based on the ratio multiplied with imagesx() and imagesy() - values return after the rotation
Set "fake" width and height when image has been rotated, so the resizing would work in proportion to the original image.
I hope this would help anyone who's struggling with the same issue I've struggled with (a few hours to much)!

PHP Imagick Composite Losing sRGB color quality why?

I have a simple watermark script which works well, but it seems adobe sRGB images lose color quality.
Running a watermark command via shell/imagemagick works great - no color quality lost.
Using imagick, however, dulls the color.
Here is the series of commands I use:
$image = new Imagick();
$image->readImage($this->source_path);
$watermark = new Imagick();
$watermark->readImage($this->watermark_path);
// how big are the images?
$iWidth = $image->getImageWidth();
$iHeight = $image->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
// calculate the position
$x = ( $iWidth - $wWidth ) / 2;
$y = ( $iHeight - $wHeight ) / 2;
//we have to make the transparency go to white, or it will become an awefull black color in jpeg version
$white = new Imagick();
$white->newImage($image->getImageWidth(), $image->getImageHeight(), "white");
if ($image->getImageColorspace() == Imagick::COLORSPACE_SRGB) {
$watermark->setColorspace(imagick::COLORSPACE_RGB);
$white->setColorspace(imagick::COLORSPACE_RGB);
}
$white->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
//now apply watermark
$white->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
//save
$white->writeImage($this->destination);
//save memory
$image->destroy();
$white->destroy();
I made a half-educated assumption that if I convert the other two elements (the white background, and the png overlay) to sRGB, nothing would be lost. I did that with this segment here:
if ($image->getImageColorspace() == Imagick::COLORSPACE_SRGB) {
$watermark->setColorspace(imagick::COLORSPACE_RGB);
$white->setColorspace(imagick::COLORSPACE_RGB);
}
...Still no shrimp Lieutenant Dan...
Is there any possible way around this issue? Ideally I'd like to use the shell commands, but I'd like to perfect the imagick version for those who do not have shell access in their environments.
I found the solution here: http://www.php.net/manual/ru/imagick.compositeimage.php (was like a scavenger hunt!)
The solution as stated in above link:
You might need to set the colorspace the same when composing two
images over each other
<?php
//Creating two Imagick object
$first = new Imagick('first.jpg');
$second = new Imagick('second.jpg');
// Set the colorspace to the same value
$first->setImageColorspace($second->getImageColorspace() );
//Second image is put on top of the first
$first->compositeImage($second, $second->getImageCompose(), 5, 5);
//new image is saved as final.jpg
$first->writeImage('final.jpg');
?>

Resize images of every size to fixed width and height

I tried a lot of methods to do that, but I still have a lot of troubles with that.
Is it possible to resize all images to fixed width and height?
I want every uploaded image with width>=200px and height>=260px to be resized to width=200px and height=260px, but I want to keep a bit of proportionality and if image is bigger than 200x260px to resize it proportionally and then to capture center of image 200x260px.
I just need idea to know where to start and what to do, but if you have an example I would like to see it. Thanks.
If you want to trim the image you can do in the following way:-
//Your Image
$imgSrc = "image.jpg";
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
To begin writing the function, we have to declare it as such… Then we have to throw in our attributes. We want to restrict our image, so we have to let the function know the dimensions to which we want to restrict it, and we have to know what the original image size is to begin with (we’ll get to that part in a second).
<?php
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the
formula accordingly...this is so this script will work
dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you
can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";
}
?>
Before we take our new function on a test drive, we need to get the width and height of the image that we want to display. There is a magical command in PHP called getimagesize(). This command, used properly, will return the image width, height, type, and even the width and height in HTML image tag format (width=”x” height=”y”).
$mysock = getimagesize("images/sock001.jpg");
Now, $mysock is an array that holds vital information about the particular image we want to display. In index 0, we have the width ($mysock[0]), and in index 1, we have the height ($mysock[1]). That’s really all we need, in order to get what we want done. Want to see the function… well, function? Here we go!
Let’s say you want to display a list of your beautiful socks, but you want room on the page to show them neatly in a row, and to do that they cannot be larger than 150 pixels tall or wide.
<?php
//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");
?>
<!-using a standard html image tag, where you would have the
width and height, insert your new imageResize() function with
the correct attributes -->
<img src="images/sock001.jpg" <?php imageResize($mysock[0],
$mysock[1], 150); ?>>
That’s it! Now, no matter what the original file size, it will be restricted to no more than 150 pixels in width or height (or whatever you specify).

Categories