PHP - imagecopyresampled: cut off x pixels from bottom of image - php

I need to cut off 20 pixels from the bottom.
This is what I've tried:
if($mime == 'image/png'){
$image = imagecreatefrompng($entry);
}else{
$image = imagecreatefromjpeg($entry);
}
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
$new_width = $width;
$new_height = $height - 20;
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, $dst_x, $dst_y, $src_x, $src_y, $new_width, $new_height, $width, $height);
imagejpeg($image_p, 'crop.jpg', 100);
I get the same image but just resized in height by -20px and without bottom cut off, what is wrong here?

OK I'm using this beautiful alternative to that idiotic bs above:
$im2 = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => $width, 'height' => $new_height]);
imagejpeg($im2, 'crop.jpg', 100);

Related

watermark upload image without resize image width height

so my following code is working like if i upload image it will resize image to 720x450 and then watermark it. but i wish not to modify the width and height and put the watermark at the bottom right of an image of any size
if someone could help me out here?
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 720; $height = 450;
$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, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
appreciate your help and time.
You are providing manual height and width, Just assign the original height ans width of image
$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth; $height = $oheight;
$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, 90);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
Try this will work as you expected.
for more info check here http://php.net/manual/en/image.examples-watermark.php

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

center an image when resizing an image and before imagefill

I'm new to php, actually new to everything in general. How do i center an image before imagefill? Right now the image goes to the left if the ratio is smaller than the new ratio and everything on the right is filled in white.
I've tried imagecopyresampled($tn, $img_source, 0, 0, -$new_width/2, 0, $new_width, $new_height, $width, $height); well that just cuts everything in half of what the position is.
if ( $image_ratio <= $mod_ratio ){
$new_height = $mod_height;
$new_width = $width / ($height / $mod_height);
}
else {
$new_width = $mod_width;
$new_height = $height / ($width / $mod_width);
}
//blah blah
$tn= imagecreatetruecolor($mod_width, $mod_height);
$whiteBackground = imagecolorallocate($tn, 255, 255, 255);
imagefill($tn,0,0,$whiteBackground);
imagecopyresampled($tn, $img_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

Extend canvas around image

I've got this code. My problem, that it doesn't work correctly. It extends canvas around image, but image "deformed". I don't know where is it the problem?!
$width = 100;
$height = 80;
//$source_width = 50;
//$source_height = 30;
if ($width > $source_width AND $height > $source_height)
{
$width_new = $width;
$height_new = $height;
$dst_x = ($width - $source_width)/2;
$dst_y = ($height - $source_height)/2;
}
$img = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $width_new, $height_new, $source_width, $source_height);
Your destination height and width are larger than the source height and width. Make them the same.
You should only use the larger height and width for the canvas (the white)
For example:
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $width_new, $height_new, $source_width, $source_height);
Should be:
imagecopyresampled($img, $source_image, $dst_x, $dst_y, 0, 0, $source_width, $source_height, $source_width, $source_height);

Cropping images with Php

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

Categories