Image cropping with fixed aspect ratio - php

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);

Related

How create image with a blur image background in php

Im trying to create with php a 4:3 image with any image uploaded by user. No matter the image original size, i want to fill the background with a blurred copy of the same image.
This is the code i use (from István Ujj-Mészáros):
function resize($source_image, $destination, $tn_w, $tn_h, $quality = 90) {
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $tn_w / $src_w;
$y_ratio = $tn_h / $src_h;
if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $tn_h) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $tn_w;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $tn_h;
}
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
// This code fill with green color
//$backgroundColor = imagecolorallocate($final, 0, 255, 0);
//imagefill($final, 0, 0, $backgroundColor);
imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
// This code generates a blurred image
# ****************************************************
//for ($x=1; $x <=2; $x++){
// imagefilter($final, IMG_FILTER_GAUSSIAN_BLUR, 999);
//}
//imagefilter($final, IMG_FILTER_SMOOTH,99);
//imagefilter($final, IMG_FILTER_BRIGHTNESS, 10);
# ****************************************************
if (imagejpeg($final, $destination, $quality)) {
return true;
}
return false;
}
// targetFilePath contains the folder an filename
resize($targetFilePath,$targetFilePath,640,480,90);
The result is like this image:
my result until now
What do i need?
The result that i hope
Please any idea will be welcome.
Thank you in advance!!!
I enhanced #mhuenchul code and it works now whatever the image size is.
function image_blurred_bg($image, $dest, $width, $height){
try{
$info = getimagesize($image);
} catch (Exception $e){
return false;
}
$mimetype = image_type_to_mime_type($info[2]);
switch ($mimetype) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image);
break;
case 'image/gif':
$image = imagecreatefromgif($image);
break;
case 'image/png':
$image = imagecreatefrompng($image);
break;
default:
return false;
}
$wor = imagesx($image);
$hor = imagesy($image);
$back = imagecreatetruecolor($width, $height);
$maxfact = max($width/$wor, $height/$hor);
$new_w = $wor*$maxfact;
$new_h = $hor*$maxfact;
imagecopyresampled($back, $image, -(($new_w-$width)/2), -(($new_h-$height)/2), 0, 0, $new_w, $new_h, $wor, $hor);
// Blur Image
for ($x=1; $x <=40; $x++){
imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($back, IMG_FILTER_SMOOTH,99);
imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
$minfact = min($width/$wor, $height/$hor);
$new_w = $wor*$minfact;
$new_h = $hor*$minfact;
$front = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($front, $image, 0, 0, 0, 0, $new_w, $new_h, $wor, $hor);
imagecopymerge($back, $front,-(($new_w-$width)/2), -(($new_h-$height)/2), 0, 0, $new_w, $new_h, 100);
// output new file
imagejpeg($back,$dest,90);
imagedestroy($back);
imagedestroy($front);
return true;
}
There are many approaches, but I would suggest you use imagecopymerge instead of imagecopy. You provide the destination and source coordinates as well as the source size and voila!
However note that this way you won't keep transparency (for GIF/PNG, if present). To do it, have a look at this comment of Sina Salek in the PHP documentation: PNG ALPHA CHANNEL SUPPORT for imagecopymerge().
But I would agree with #LeeKowalkowski - in the long run, you should consider migrating to ImageMagick for many reasons - image quality above all.
EDIT: I forgot to mention, before merging, set transparent colour (the extended canvas) with imagecolortransparent. Then, Sina Salek's comment in PHP documentation (the link above).
Ok, i did my work and studied more about php image commands. I wrote a solution that works very well.
<?php
$image = "01.jpg";
image_web($image, 700, 525); // I need final images 700x525 (4:3)
echo '<img src="00.jpg"/>';
function image_web($image, $width, $height){ // Get the image source and the final desire dimensions
$info = getimagesize($image);
//$mimetype = $info['mime']; // other way to get mimetype
$mimetype = image_type_to_mime_type($info[2]);
$allowTypes = array('image/jpeg','image/png','image/gif');
if(in_array($mimetype, $allowTypes)){
switch ($mimetype) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image);
break;
case 'image/gif':
$image = imagecreatefromgif($image);
break;
case 'image/png':
$image = imagecreatefrompng($image);
break;
default:
die('Invalid image type.');
}
} else {
echo 'Is not a image';
}
$image2 = $image; // Save a copy to be used for front image
$wor = imagesx($image); // Get original image width
$hor = imagesy($image); // Get original image height
if ($hor >= $wor){ // If is vertical*******************************************************************************************************
if ($wor >= $width){ // If image source is bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $wor, $hor, $wor, $hor);
} else { // If image source is not bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$hnu = ($hor*$width)/$wor;
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
} else { // If is portrait rectangular****************************************************************************************************
$ratio = $wor/$hor;
if($ratio > 1.3333333){ // If is portrait larger than 4:3
$wnu = ($wor*$height)/$hor;
$wcenter = ($wnu/2)-($width/2); // center image in width
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, -$wcenter, 0, 0, 0, $wnu, $height, $wor, $hor);
} else { // If portrait is not larger than 4:3
$hnu = ($wor*$height)/$hor;
$hcenter = ($hnu/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
}
// Blur Image
for ($x=1; $x <=40; $x++){
imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($back, IMG_FILTER_SMOOTH,99);
imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
// Getting the dimensions of the image
$src_w = imagesx($image2);
$src_h = imagesy($image2);
// Do some math to figure out which way we'll need to crop the image
// to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $width / $src_w;
$y_ratio = $height / $src_h;
if (($src_w <= $width) && ($src_h <= $height)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $height) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $width;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $height;
}
$front = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($front, $image2, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
if ($new_h >= $new_w){ // If is vertical image
$wctr = ($new_w/2)-($width/2);
imagecopymerge($back, $front,-$wctr, 0, 0, 0, $new_w, $new_h, 100);
} else { // if is portrait
$hctr = ($new_h/2)-($height/2);
imagecopymerge($back, $front,0, -$hctr, 0, 0, $new_w, $new_h, 100);
}
// output new file
imagejpeg($back,'00.jpg',90);
imagedestroy($back);
imagedestroy($front);
//*********************************************************************************
/**
Do other actions like send ajax responses, save in database, etc
**/
}
?>

Adding logo as watermark, watermark poor quality

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);

PHP: Black strip on image while using GD library

I have a little problem with GD library in PHP - I resize image and then I want crop it to 320px (width) / 240px (height). Let me say that resized image is 320px/300px. When I crop it, a 1-px black strip appears on the bottom of the image - I don't know why.
I'm using imagecrop, imagecreatefromjpeg and imagecopyresampled
Here's the example:
Thanks for your time.
The code
$filename = '../store/projects/project-123.jpg';
$mime = mime_content_type($filename);
list($w, $h) = getimagesize($filename);
$prop = $w / $h;
$new_w = 0;
$new_h = 0;
if ($prop <= 4/3) {
$new_w = 320;
$new_h = (int)floor($h*($new_w/$w));
} else {
$new_h = 240;
$new_w = (int)floor($w*($new_h/$h));
}
$thumb = imagecreatetruecolor($new_w, $new_h);
if (strcmp($mime,'image/png') == 0) {
header('Content-Type: image/png');
$source = imagecreatefrompng($filename);
} else {
header('Content-Type: image/jpeg');
$source = imagecreatefromjpeg($filename);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
$filename = '../store/projects-thumbs/project-123.jpg';
$crop_data = array('x' => 0 , 'y' => 0, 'width' => 320, 'height'=> 240);
$thumb = imagecrop($thumb, $crop_data);
imagejpeg($thumb, $filename, 100);
imagedestroy($thumb);
imagedestroy($source);
imagecrop() has a known bug that causes the black bottom border to be added.
You can work around the problem using imagecopyresized(). See my answer to another SO question asking for an imagecrop() alternative.

Adding Watermark to Ajax Crop

Im using Ajax Crop from enter link description here and I'd like to add a watermark to the script. The following is my script.
WATERMARK
$image_path = "watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 500; $height = 100;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
UPLOAD AND RESIZE PART
function resizeThumb($arr){
$date = md5(time());
$arr['temp_uploadfile'] = $arr['img_src'];
$arr['new_uploadfile'] = $arr['uploaddir'].strtolower($date).'.jpg';
asidoImg($arr);
exit;
}
I tried adding
$arr = watermark_image($arr['temp_uploadfile'],
$arr['uploaddir'].strtolower($date).'.jpg');
in place of
$arr['new_uploadfile'] = $arr['uploaddir'].strtolower($date).'.jpg';
but this didnt work.
Could someone help me?
Download the files and test
It turns out the script uses ASIDO and it has a built in WATERMARK TOOL.
In func.php I done the following.
function asidoImg2($arr){
include('asido/class.asido.php');
asido::driver('gd');
$height = $arr['height'];
$width = $arr['width'];
$x = $arr['x'];
$y = $arr['y'];
// process
$i1 = asido::image($arr['temp_uploadfile'], $arr['new_uploadfile']);
// fit and add white frame
if($arr['thumb'] === true){
Asido::Crop($i1, $x, $y, $width, $height);
asido::watermark($i1, 'watermark.png', ASIDO_WATERMARK_MIDDLE_LEFT, ASIDO_WATERMARK_SCALABLE_ENABLED, 1);
}
else{
Asido::Frame($i1, $width, $height, Asido::Color(255, 255, 255));
asido::watermark($i1, 'watermark.png', ASIDO_WATERMARK_MIDDLE_LEFT, ASIDO_WATERMARK_SCALABLE_ENABLED, 1);
}
// always convert to jpg
Asido::convert($i1, 'image/jpg');
$i1->Save(ASIDO_OVERWRITE_ENABLED);
$data = array(
'photo'=> $arr['new_uploadfile']
);
// echo $user_id;
// delete old file
echo $data['photo'];
}

download image from remote source and resize then save

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.

Categories