i am creating a new feature on my site that allow people to send postcard to friends. in this section they can choose the image they want to send (they already uplaoded the image to their profile -> my pictures section)
i am using the php function to create the text that goes on the right but how can i add another image to this image with the text?
i use imagettftext to create the text, imagecreatefromjpeg to open the main image (see below) and imagedestroy when im done
thanks
i am using this postcard:
First you will have to crop the image to fit in your postcard.
Based on your image here's what you have to do:
<?php
$sourceImage = './postcard-template.jpg';
$uploadedImage = '/path/to/image/hong-kong2.jpg'; // let's get hong kong as example
$mime = '';
$font = '/path/to/font/arial.ttf';
function CroppedThumbnail($source, $width, $height, &$mime) {
$data = getimagesize($source);
$sourceWidth = $data[0];
$sourceHeight = $data[1];
$mime = $data['mime'];
$image = imagecreatefromjpeg($source);
$sourceRatio = $sourceWidth/$sourceHeight;
if (($width/$height) > $sourceRatio) {
$newHeight = $width/$sourceRatio;
$newWidth = $width;
}
else {
$newWidth = $height*$sourceRatio;
$newHeight = $height;
}
$croppedImage = imagecreatetruecolor(round($newWidth), round($newHeight));
imagecopyresampled($croppedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $croppedImage, 0, 0, (($newWidth/2)-($width/2)), (($newHeight/2)-($height/2)), $width, $height, $width, $height);
imagedestroy($croppedImage);
imagedestroy($image);
return $thumb;
}
// Create the cropped image first
$newThumb = CroppedThumbnail($uploadedImage,240,315, $mime);
switch($mime) {
case 'image/gif':
$image = imagecreatefromgif($sourceImage);
break;
case 'image/jpeg':
$image = imagecreatefromjpeg($sourceImage);
break;
case 'image/png':
$image = imagecreatefrompng($sourceImage);
break;
default:
// error or stop script
break;
}
$message = "this is some text\nsome other text\ntext text";
imagettftext($image, 21, 0, 320, 255, imagecolorallocate($image, 0, 0, 0), $font, $message);
imagecopy($image, $newThumb, 40, 40, 0, 0, 240, 315);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
For example I use this image ( needs to be cropped ) :
then it will output:
Use imagecopymerge to copy the photo onto the post card
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
Related
I am adding a transparant logo as watermark over an image using PHP. However, in the result the logo has poor quality (the image that is under it is high quality, so it's just the watermark). This is the code I use (its about the last 3 lines):
header("Content-Type: image/png");
$photo = imagecreatefromjpeg('photos/'.$photo['image']);
$height = imagesx($photo);
$width = imagesx($photo);
if ($width > $_POST['width']) {
$r = $width / $_POST['width'];
$newwidth = $width / $r;
$newheight = $height / $r;
}
$image = imagecreatetruecolor($width, $height);
$image2 = imagecopyresampled($image, $photo, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$position = explode(" ", $_POST['background']);
$image3 = imagecrop($image, [
'x' => str_replace(array('-', 'px'), array('', ''), $position[0]),
'y' => str_replace(array('-', 'px'), array('', ''), $position[1]),
'width' => $_POST['width'],
'height' => $_POST['height']
]);
$stamp = imagecreatefrompng('img/logo.png');
imagecopyresized($image3, $stamp, 0, 0, 0, 0, 147, 50, imagesx($stamp), imagesy($stamp));
imagepng($image3, "created/".time().".png", 9);
imagecopyresized will copy and scale and image. This uses a fairly primitive algorithm that tends to yield more pixelated results.
a simple example for a better quality is:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
you should have a look at this post here
use quality of image from 1-100.
imagejpeg($image, $new_image_name, 99);
I have a problem of image quality after a resize. Could you help me to find a solution to my problem?
This is a page where an image has been resized
goo.gl/5mYxxb , 460*275 and this the code that I am using to resize :
$newwidth = 460;
$newheight = 275;
$newwidth1 = 146;
$newheight2 = 88;
$temp = imagecreatetruecolor($newwidth, $newheight);
$black2 = imagecolorallocatealpha($temp,0,0,0,65);
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg($path);
imagecopyresampled($temp, $src, 0,0,0,0, $newwidth, $newheight, $width, $height);
Imagettftext($temp, 16, 0, $start_x, $start_y, $black2, $font , "text");
imagejpeg($temp, $path,95);
imagedestroy($temp);
$temp2 = imagecreatetruecolor($newwidth1, $newheight2);
imagecopyresampled($temp2, $src, 0,0,0,0, $newwidth1, $newheight2, $width, $height);
Imagettftext($temp2, 8, 0, $start_x2, $start_y2, $black2, $font , "text");
imagejpeg($temp2,$pathFile ,90);
imagedestroy($temp2);
imagedestroy($src);
I'm trying to take in two sets of x-y co-ordinates from an image in a 7:9 aspect ratio and replace the original image with the cropped section in a 280x360 image, but it's not working. It's not throwing up any errors but the image replacement after cropping doesn't seem to work. Echoing data tells me it takes in everything up to the imagecopyresampled code.
$formDatax1=$_POST["x1"];
$formDatax2=$_POST["x2"];
$formDatay1=$_POST["y1"];
$formDatay2=$_POST["y2"];
$filename='http://pathtofiles/path/photo/'.$a_photo;
$image_info = getimagesize($filename);
switch(strtolower($image_info['mime'])){
case 'image/png' : $image = imagecreatefrompng($filename); $imgtype='png'; break;
case 'image/jpeg': $image = imagecreatefromjpeg($filename); $imgtype='jpg'; break;
case 'image/gif' : $image = imagecreatefromgif($filename); $imgtype='gif'; break;
default: die();
}
$resized_width = ((int)$formDatax2) - ((int)$formDatax1);
$resized_height = ((int)$formDatay2) - ((int)$formDatay1);
$resized_image = imagecreatetruecolor(280, 360);
imagecopyresampled($resized_image, $image, 0, 0, (int)$formDatax1, (int)$formDatay1, 280, 360, $resized_width, $resized_height);
if ($imgtype=='png') {
imagepng($resized_image, $filename);
}
if ($imgtype=='jpg') {
imagejpeg($resized_image, $filename);
}
if ($imgtype=='gif') {
imagejpeg($resized_image, $filename);
}
echo '<script type="text/javascript">alert("Image cropped!"); </script>';
exit();
You're not specifying a new value for $filename. The http[s] URL wrappers can retrieve a file, but not write. You'll need to specify a local filesystem location to save the image to.
This solution is from PHP cookbook 3rd edition
Use the ImageCopyResampled() function, scaling the image as needed.
To shrink proportionally:
$filename = __DIR__ . '/php.png';
$scale = 0.5; // Scale
// Images
$image = ImageCreateFromPNG($filename);
$thumbnail = ImageCreateTrueColor(
ImageSX($image) * $scale,
ImageSY($image) * $scale);
// Preserve Transparency
ImageColorTransparent($thumbnail,
ImageColorAllocateAlpha($thumbnail, 0, 0, 0, 127));
ImageAlphaBlending($thumbnail, false);
ImageSaveAlpha($thumbnail, true);
// Scale & Copy
ImageCopyResampled($thumbnail, $image, 0, 0, 0, 0,
ImageSX($thumbnail), ImageSY($thumbnail),
ImageSX($image), ImageSY($image));
// Send
header('Content-type: image/png');
ImagePNG($thumbnail);
ImageDestroy($image);
ImageDestroy($thumbnail);
To shrink to a fixed-size rectangle:
// Rectangle Version
$filename = __DIR__ . '/php.png';
// Thumbnail Dimentions
$w = 50; $h = 20;
// Images
$original = ImageCreateFromPNG($filename);
$thumbnail = ImageCreateTrueColor($w, $h);
// Preserve Transparency
ImageColorTransparent($thumbnail,
ImageColorAllocateAlpha($thumbnail, 0, 0, 0, 127));
ImageAlphaBlending($thumbnail, false);
ImageSaveAlpha($thumbnail, true);
// Scale & Copy
$x = ImageSX($original);
$y = ImageSY($original);
$scale = min($x / $w, $y / $h);
ImageCopyResampled($thumbnail, $original,
0, 0, ($x - ($w * $scale)) / 2, ($y - ($h * $scale)) / 2,
$w, $h, $w * $scale, $h * $scale);
// Send
header('Content-type: image/png');
ImagePNG($thumbnail);
ImageDestroy($original);
ImageDestroy($thumbnail);
I'm using Jquery plugin ImgAreaSelect with PHP 5.3.9 with GD 2.0.34.
Following some examples from the plugin, I added a form that gives X and Y values from where I start selecting the image till the end of the selection.
This is going OK, because I recieve the values correctly, but I cannot crop the image. Followed some examples/tutorials but always failed.
Here is my PHP code:
$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = $x2 - $x1; //getting the width for the new image
$h = $y2 - $y1; //getting the height for the new image
$src_img = "path/image";
$format = end(explode(".", $src_img)); //taking the image format (jpg, png, gif)
$size = getimagesize($src_img);
switch($format) {
case "jpg":
$copy = imagecreatefromjpeg($src_img);
$new = ImageCreateTrueColor($w, $h);
imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $size[0], $size[1]);
header('Content-type: image/jpeg');
imagejpeg($new);
break;
}
I would like to know if there's something wrong (most probably).
Thanks for all and taking your time to help.
imagecopyresampled($new, $copy, $x1, $y1, 0, 0, $w, $h, $size[0], $size[1]);
http://php.net/manual/en/function.imagecopyresampled.php
In other words, imagecopyresampled() will take a rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).
in other words, you need to change it to:
imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);
Anyway you can try this code also
<?php
$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = ( $x2 - $x1 ); //getting the width for the new image
$h = ( $y2 - $y1 ); //getting the height for the new image
$src = "path_to_file";
$info = getimagesize( $src );
switch( $info[2] ) {
case IMAGETYPE_JPEG:
$copy = imagecreatefromjpeg( $src );
$new = imagecreatetruecolor( $w, $h );
imagecopyresampled( $new, $copy, 0, 0, $x1, $y1, $info[0], $info[1], $w, $h );
header( 'Content-type: image/jpeg' );
imagejpeg( $new );
break;
default:
break;
}
?>
//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,
Resize and crop image from center with PHP
$max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");
thanks to all, #Deepanshu gave me a link wich I saw a piece of code, but somehow strange:
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
the last $src_w and $src_h I had to put the new width and the new height, instead the width and height of the original image.
so, the final code working properly is:
imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $w, $h);
everything in my code is working great for creating a thumbnail image of an uploaded picture.
now all i need to do is crop the $thumb from the center of the image into a square shape (50x50)
heres my function so far
$ext = end(explode('.', $_FILES['profile_photo']['name']));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')
{
$tmp = $_FILES['profile_photo']['tmp_name'];
if ($ext=='jpg' || $ext=='jpeg')
$src = imagecreatefromjpeg($tmp);
else if ($ext=='png')
$src = imagecreatefrompng($tmp);
else
$src = imagecreatefromgif($tmp);
list($width,$height) = getimagesize($tmp);
$thumb_width = 50;
$thumb_height = ($height/$width) * $thumb_width;
$thumb_tmp = imagecreatetruecolor($thumb_width, $thumb_height);
$full_width = 200;
$full_height = ($height/$width) * $full_width;
$full_tmp = imagecreatetruecolor($full_width, $full_height);
imagecopyresampled($thumb_tmp, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagecopyresampled($full_tmp, $src, 0, 0, 0, 0, $full_width, $full_height, $width, $height);
imagejpeg($thumb_tmp, 'images/profile/'.$user['id'].'_'.time().'_thumb.'.$ext, 100);
imagejpeg($full_tmp, 'images/profile/'.$user['id'].'_'.time().'_full.'.$ext, 100);
imagedestroy($src);
imagedestroy($thumb_tmp);
imagedestroy($full_tmp);
// delete old image from server if it is not none.png
}
any help would be greatly appreciated! i know that it has something to do with imagecopyresampled but i can't figure out the math for the cropping from the center of the image. i want this to be my own function so please dont recommend me using other peoples classes.
Right after $full_tmp = imagecreatetruecolor($full_width, $full_height);, add...
if ($thumb_width > $thumb_height) {
$thumb_offset = array('x' => ($thumb_width/2 - 25), 'y' => 0);
} else {
$thumb_offset = array('x' => 0, 'y' => ($thumb_height/2 - 25));
}
$square_tmp = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($square_tmp, $src, 0, 0, $thumb_offset['x'], $thumb_offset['y'], 50, 50, $width, $height);
Then save and destroy the temp like the other two images.
Take a look at the parameters that should be passed to imagecopyresampled, as per the PHP manual:
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
From the third parameter on, you basically define how a rectangle on the source image maps to a rectangle on the destination image.
So the first thing you have to do is calculate the rectanle (x, y, width and height) which defines the visible area of your original image. These will be the 5th, 6th, 9th and 10th parameters to the function, respectively.
For the destination rectangle, use 0,0 for x,y, and $thumb_width,$thumb_height for w,h, just as you are currently doing.