I have been trying to implement a rating display procedure to rating in form of stars as passed by GET in a php file.
Here's the code for rate.php :
$filename= "rating.png";
$rating = $_GET['rating'];
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefrompng($filename);
$dst_im = imagecreatefrompng("rating_back.png");
$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = $w * $rating / 5; // width
$src_h = $h; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
header("Content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
$rating is a float value as per the GET request.
But when I try to implement rate.php?rating=4.3 using the following images:
"rating.png":
"rating_back.png":
I get the following result :
What could be the possible errors which are leading to an abnormal background in the final image?
Please give the possible solutions also.
Thanks in advance :)
It looks like those are 24 bit PNG images with transparency. You need to tell GD to save the transparency info too. Add:
imagesavealpha($dst_im, true);
before rendering your image
Related
I am trying to crop an image on PHP. I saw that there is a function for doing that on PHP: imagecrop. Here is the code in which I am trying to crop the image.
$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored);
Here I want that the crop of the image starts in the left corner of the image and get the values of width and height that I put above.
But it just resize my image, not crop it. What am I doing wrong?
EDIT:
I tried also with the function imagecopyresampled. Here it is what I have tried:
dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.
src_image(Source image link resource) = image; //Here the original image.
//Here 0,0 because I want that the new image crop starts in the left corner of the original image.
dst_x(x-coordinate of destination point) = 0;
dst_y(y-coordinate of destination point) = 0;
//Here 0,0 because I want that the crop starts on the 0,0 of the original image
src_x(x-coordinate of source point) = 0;
src_y(y-coordinate of source point) = 0;
dst_w(Destination width) = 150; //The new width of the crop image.
dst_h(Destination height) = 150; //The new height of the crop image.
src_w(Source width) = 500; //The original width of the image.
src_h(Source height) = 500; //The original height of the image.
So finally the function will be like:
$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h );
I just have problems in this function it is why avoid the rest (save and imagetruecolor and the rest...)
This function give to me the result expected but the new image it is black, why?
Thanks in advance!
try this:
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
Alternatively you could use the imagecrop function of imagick.
Finally, I got the solution of my problem. I had to put the same value of dst_w and src_w. The same to dst_h and src_h. ¡¡And it works!!
The final solution it is the following:
$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);
This script helps me crop and save images. every time i save an image it saves has crop.jpg .. can i save it in a different name? i have an input field with image name.. "$_POST['imgname']" how can i use this has image name for the cropped image?
<?php
$canvasImg = $_POST['img'];
$data = base64_decode($canvasImg);
$File = "captured.jpg";
$Handle = fopen($File, 'w');
fwrite($Handle, $data);
fclose($Handle);
$src_image = imagecreatefrompng($File);
$width = imagesx($src_image);
$height = imagesy($src_image);
$dst_x = 0;
$dst_y = 0;
$src_x = $width*0.350; // Crop Start X
$src_y = $height*0.165; // Crop Srart Y
$dst_w = $width*0.294; // Thumb width
$dst_h = $height*0.470; // Thumb height
$src_w = $dst_w; // $src_x + $dst_w
$src_h = $dst_h; // $src_y + $dst_h
$image = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($image, false);
$col=imagecolorallocatealpha($image,255,255,255,127);
imagefilledrectangle($image,0,0,$dst_w, $dst_h,$col);
imagealphablending($image,true);
imagecopyresampled($image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);
imagepng($image, "images/crop.jpeg");
imagedestroy($image);
?>
If I understand your question correctly, you just need to make a new filename for the imagepng() to use.
This would seem a simple solution:
$canvasImg = $_POST['img'];
$data = base64_decode($canvasImg);
$File = "captured.jpg";
...
// create a new filesname for the cropped image in $save_to
$save_to = 'images/';
$save_to .= str_replace(array('.jpg','.png'), '', $data ) // remove original extension if it existed
$save_to .= '_crop.jpg';
// use this new name to save the cropped image
imagepng($image, $save_to);
However your code does not seem to make much sence. You accept an image name in $_POST['img'] then save it to a file called capured.jpg and then open it as a .png in $src_image = imagecreatefrompng($File);
I assume you must only be accepting .png files, and in that case why save it as a .jpg.
See what I mean, there appears to be a whole lot of confusion in your code.
I have transparent image that I want to overlay over the entire image to give it the boarder effect. In this code it crops an existing image. It merges the two but the final over the top is not showing the alpha. How can I fix this?
<?
$dst_x = 0; // X-coordinate of destination point.
$dst_y = 0; // Y --coordinate of destination point.
$src_x = 163; // Crop Start X position in original image
$src_y = 0; // Crop Srart Y position in original image
$dst_w = 469; // Thumb width
$dst_h = 296; // Thumb height
$src_w = 469; // $src_x + $dst_w Crop end X position in original image
$src_h = 296; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions.( to merge with the original image )
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
// Get original image
$src_image = imagecreatefromjpeg("http://www.ucatholic.com/wp-content/uploads/2012/10/Sallixtus-631x295.jpg");
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, "images/crop.jpg");
$src = imagecreatefrompng('http://www.EdVizenor.com/images/saintCover.png');
/// THIS LINE NOT WORKING - - // Copy and merge
imagecopymerge($dst_image, $src, 0, 0, 0, 0, 469, 296, 100);
header('Content-Type: image/png');
imagegif($dst_image);
?>
Dont use imagecopymerge, use imagecopy, then it will works normally and your watermark will be shown with alpha.
imagecopy($dst_image, $src, 0, 0, 0, 0, 469, 296);
If used the imagecopy instead of imagecopymerge you will lose control over alpha channel.
I think you could use Jaguar , if you want a real fast overlay action using the Gd extension.
see my answer Here if you want to see how
if you want a brushed border you can use Jaguar like this:
use Jaguar\Canvas,
Jaguar\Drawable\Border;
$canvas = new Canvas('your image');
$brush = new Canvas('style image');
$border = new Border(20);
$border->draw($canvas,$brush);
$canvas->save('path to save')->show();
The following code is working without any error, but my problem is when i create a thumbnail some times thumbnail are non understandable one ( some conditions such as width is very larger than height ) i also tried a code for calculate height automatically.But it won't perfectly works. I want a code which creates a understandable thumbnail every time.(cropped thumbnail can be generated )
function make_thumb($src, $dest, $desired_width)
{
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
//even if height is calculated automatically using
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagejpeg($virtual_image, $dest);
}
You can use the Class SimpleImage, like:
// Usage:
// Load the original image
$image = new SimpleImage('lemon.jpg');
// Resize the image to 600px width and the proportional height
$image->resizeToWidth(600);
$image->save('lemon_resized.jpg');
You can find this class here on github https://gist.github.com/miguelxt/908143
I've written a script to make thumb of landscape or portrait images. May be this will help you
<?php
$thumbWidth = 200; // can change it to whatever required
$thumbHeight = 200; // can change it to whatever required
$img = imagecreatefromstring(file_get_contents('SAM_1883.JPG'));
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$imgStart_x = 0;
$imgStart_y = 0;
$imgEnd_x = $imgWidth;
$imgEnd_y = $imgHeight;
if($imgWidth > $imgHeight){
$diff = $imgWidth - $imgHeight;
$imgStart_x = $diff / 2;
$imgEnd_x = $imgWidth - $diff;
}else{
$diff = $imgHeight - $imgWidth;
$imgEnd_y = $imgHeight - $diff;
}
$dest = imagecreatetruecolor($thumbHeight,$thumbHeight);
imagecopyresized($dest, $img, 0, 0, $imgStart_x, $imgStart_y, $thumbWidth, $thumbHeight, $imgEnd_x, $imgEnd_y);
imagePNG($dest,'abc'.rand(0,9999).'.png');
?>
However you can change the source, thumbWidth, thumbHeight and destination of thumb as per your requirement.
https://github.com/lencioni/SLIR can resize your image on the fly. It will cache the image on the server as well as make it cacheable on the browser and proxy servers. The resizing happens when loading the image, not when loading HTML so your HTML is loading faster.
I want to take out the text in the bottom of an image. How can I cut it from bottom ...say 10 pixels to cut from bottom.
I want do this in PHP. I have lots of images that have text in the bottom.
Is there a way to do it?
Here you go.
To change the name of the image, change $in_filename (currently 'source.jpg'). You can use URLs in there as well, although obviously that will perform worse.
Change the $new_height variable to set how much of the bottom you want cropped.
Play around with $offset_x, $offset_y, $new_width and $new_height, and you'll figure it out.
Please let me know that it works. :)
Hope it helps!
<?php
$in_filename = 'source.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 15;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
?>
You may use the GD Image Library to manipulate images in PHP. The function you're looking for is imagecopy(), which copies part of an image onto another. Here's an example from PHP.net that does roughly what you describe:
<?php
$width = 50;
$height = 50;
$source_x = 0;
$source_y = 0;
// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);
// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);
// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);
?>
To crop the source image, change the $source_x and $source_y variables to your liking.