PHP GD: Outputted Image is the wrong size - php

I'm starting back into GD library and I'm having a simple problem. The image that is outputted is the wrong size. It's always the same size, no matter what size I try to make it. Here's the code:
$pW = 15;
$pH = 30;
$numW = 100;
$numH = $numW/2;
$sizeW = $pW*$numW;
$sizeH = $pH*$numH;
header('Content-type: image/jpeg');
$image = imagecreatetruecolor($sizeW, $sizeH);
$black = imagecolorallocate($image,0,0,0);
imagejpeg($image);
imagedestroy($image);
This simple code is trying to make an image that is 1500px wide x 1500px high. But when it outputs, it's always showing width and height of 527px;.
Here's the output:
<img style="-webkit-user-select: none; cursor: zoom-in;" src="http://sample.com/example.php" width="527" height="527">
As you can see the width and height is 527. Even when I change the height and width manually on the imagecreatetruecolor() it outputs always to 527.
Any ideas?

Related

JCrop cropping the wrong area

I need some help with my implementation of JCrop. I tried to follow the manual but whenever I select an area, it's not actually selecting what I have selected. Instead it's taking some area outside my selection. Sometimes, it's taking the area almost properly (I am thinking because of the photo size) but mostly it's taking wrong area. Please help. Here's my code:
Note $photo is the uploaded image. It's being uploaded properly so no issue on that.
The 120 x 120 is the pixels I want to save the uploaded pictures to so as to save storage space. Also, here's the css of the <img> element where I display the image for cropping:
max-width: 100%;
height: auto;
display: inline-block;
position: relative;
Here's my php code:
$twidth = 120;
$theight = 120;
$quality = 90;
$src = imagecreatefromjpeg($photo);
$dst = ImageCreateTrueColor($twidth, $theight);
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
imagecopyresampled($dst, $src, 0, 0, $x, $y, $twidth, $theight, $w, $h);
imagejpeg($dst, $photo, $quality);
The problem here is that you are resizing the image box with css. This is creating difficulties for the script to find the correct area.
Instead you should limit the box width/height with Jcrop function as below:
$('#cropbox').Jcrop({
boxWidth: 480, //Maximum width you want for your bigger images
boxHeight: 480, //Maximum Height for your bigger images
...
});
If you use this Jcrop setting it would take the wanted area, otherwise it is not working as expected.
Best wishes

Add black bars to create a 16x9 image

I have a jpg on my server. I use
imagecreatefromjpeg($imgPath);
to open it. I want to make it a 16x9 image by adding black bars to either the top+bottom or left+right. (Think background-size: contain; background-position: center;) This is all I have:
$img_info = getimagesize($imgPath);
I know I need to use ImageCreateTrueColor to make the blank image, imagecopyresampled to create the image, and imagejpeg to save it. But I have no idea how to put them together. Thanks!
This will do the trick:
$im=imagecreatefromjpeg ($imgPath);
$width=ImageSX($im); $height=ImageSY($im); $ratio=16/9;
$width_out=$width; $height_out=$height;
if ($height_out*$ratio<$width_out) {$height_out=floor($width_out/$ratio);} else {$width_out=floor($height_out*$ratio);}
$left=round(($width_out-$width)/2);
$top=round(($height_out-$height)/2);
$image_out = imagecreatetruecolor($width_out,$height_out);
$bg_color = ImageColorAllocate ($image_out, 0, 0, 0);
imagefill($image_out,0,0,$bg_color);
imagecopy($image_out, $im, $left, $top, 0, 0, $width,$height);
imagejpeg($image_out);
How it works: you create the $im container, and check for width and height.
After this, the script checks which side is smaller than the other (multiplied / divided by the ratio) and adjust the output size.
Calculate where the original image should be placed (center alignment) by dividing the difference between the original and the output image dimensions by 2.
Copy over the original image at the given position
Output, done.

Automatically rotate image when is width bigger than height in PHP

I asked in past, but I am not sure we understand & I still havent solution.
I need elegant solution when;
I have photo 600x800 & I need show it on my site rotated on 90 degrees, so result will be, when I print php page all photos will be verticaly automaticly.
e.g.
I have a lot of photos, two kinds: 800x600 & 600x800.
I need on my php page showed all of them 800x600 in original and all 600x800 rotated on 90 degrees.
I need some really simple solution, I am out of mind totally. Some function which can rotate images which have bigger width than height.
Thanks a lot.
Using PHP function getimagesize() you can get width and height of your image:
list($width, $height) = getimagesize($imageUrl);
Then, in your template:
<?php if($width > $height): ?>
//put css here as you want
<?php else: ?>
//put css here as you want
<?php endif; ?>
Use getimagesize for detect image width and if width is 800px add inline css like this
style="-moz-transform: rotate(90deg); -webkit-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);"
getimagesize() function will give you width and height and by using imagerotate() you can rotate that image to any desired angle.
Try this:-
<?php
// File and rotation
$filename = 'php.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

Rotate image on top of background-image

I have two images. one is a jpg image of a rotated polaroid frame polaroid.jpg. The other is just an ordinary image image.jpg.
I'm trying to rotate the image, and then put it on top of the polaroid-image, and then show the merged images as one jpg-image.
I think I'm pretty close with the following code, but I can't manage to get the transparancy working. The uncovered zone of the rotated image is black instead of transparent. What am I doing wrong? I've added a comment to the lines that are relevant for getting a transparent background for the top-image.
$bg_src = "polaroid.jpg";
$img_src = "image.jpg";
$outputImage = imagecreatefromjpeg($bg_src);
$img = imagecreatefromjpeg($img_src);
// This should create transparent background.
$bgd_color = imagecolorallocatealpha($img, 0, 0, 0, 127);
// This should assign the transparent background to the uncovered zone after rotation
$img = imagerotate($img, 10, $bgd_color);
// This should make sure the alpha transparency gets saved
imagesavealpha($img, true);
$img_x = imagesx($img);
$img_y = imagesy($img);
imagecopymerge($outputImage,$img,156,50,0,0,$img_x,$img_y,100);
header('Content-type: image/jpeg');
imagejpeg($outputImage);
imagedestroy($outputImage);
Figured it out after some heave searching. Turns out to be real simple.
I just changed this line:
imagesavealpha($img, true);
to this:
imagecolortransparent($img,$bgd_color);
yay! :)

How to crop and image before adding an overlay? (GD) (PHP)

My code so far (it creates an overlay to a youtube thumbnail):
<?php
header("Content-type:image/png");
$background=imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert=imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
$insert_x=imagesx($insert);
$insert_y=imagesy($insert);
imagecopymerge($background,$insert,5,57,0,0,$insert_x,$insert_y,100);
imagepng($background,NULL);
The output image is 120x90px, but i need it to have 90x90px.
Anyone knows how this is possible?
Thanks in advance!
http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
<?php
header("Content-type:image/png");
$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert = imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
list($current_width, $current_height) = getimagesize($background);
$left = 0;
$top = 0;
$crop_width = 90;
$crop_height = 90;
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = $background;
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagecopymerge($canvas,$insert,5,57,0,0,$current_width,$current_height,100);
imagepng($canvas);
?>
try that it should work if not comment as to otherwise.
This is taken from a function I wrote to create square thumbnails. You may find the commentary I wrote for myself helpful. Depending on your needs (i.e if you can't afford to make assumptions about the type and dimensions of incoming images) you may need to add additional checks. To avoid smashed or stretched thumbnails we take a central part of the image which is most likely to contain something distinguishable as source co-ordinates.
The basic idea is: Since imagecopyresampled or imagecopyresized also allow you to specify destination and source coordinates, you can copy only part of your original image to a new canvas and save that (or directly output to browser). To avoid smashed or stretched dimensions we take a central part of the original image which is most likely to contain distinguishable content.
//assuming you have already merged your $background image with your overlay at this point
//original dimensions
$original_width = imagesx($background);
$original_height = imagesy($background);
//new dimensions
$new_width = 90;
$new_height = 90;
//the center of the rectangular image
$center = $original_width/2, $original_height/2
//the coordinates from where to start on the original image
//assuming you want to crop the center part
$src_x = floor(($original_width/2) - ($new_width/2));
$src_y = floor(($original_height/2) - ($new_height/2));
//create a new canvas with the new desired width, height
$temp = imagecreatetruecolor(90,90);
//copy the large image to this new canvas
imagecopyresampled($temp,$background,0,0,$src_x,$src_y,$new_width,$new_height,$original_width,$original_height);
//from right to left: source image, destination image, dest x, dest y,
//source x, source y, new width, new height, original width, original height
//save the canvas as an image
imagepng($temp);
This could be improved to handle larger images by first taking a central part relative to it's size and then scaling it down to the new canvas.

Categories