I am trying to crop and upload image, I have tried following code for crop image.
$img_r = imagecreatefromjpeg('/home/user/site.com/wp-content/themes/my-theme/uploads/test.jpeg');
$new_canvas = imagecreatetruecolor(350, 350);
imagecopyresampled($new_canvas, $img_r, 0, 0, 85, 13, 350, 350, 500, 500 );
imagejpeg($new_canvas, $src);
imagedestroy($new_canvas);
I have uploaded image 500*500 and want to crop it in size 350*350 image size top X and left Y coordinate from where need to start image cropping are 85px and 13px.
The problem is that when it saved to server mean I over write same image in server and check image is cropped but not in that particular area.
It make image small and add at left and right side black space in image.
Please let me know that what I am doing wrong.
Thanks in advance.
Found answer after too much work on it, it's a simple.
just change:
$img_r = imagecreatefromjpeg('/home/user/site.com/wp-content/themes/my-theme/uploads/test.jpeg');
TO:
$img_r = imagecreatefromjpeg('site.com/wp-content/themes/my-theme/uploads/test.jpeg');
I am using directory path and there should be URI path.
Related
We're using a CMS with WideImage built in and no scope to change this, however we've come up against a situation where we need to produce 300x300 images.
However the images that are uploaded at approx 100x100 in jpg and png format with various levels of transparency.
I'm trying to create a 300x300 canvas, and place the 100x100 image inside of it - however I want both the canvas to remain transparent, and the image placed on top to keep its transparency.
I've got
$image = WideImage::loadFromFile( $this->local_path );
$canvas = WideImage::createTrueColorImage(300, 300);
$canvas_bg = $canvas->allocateColor(255, 255, 255);
$canvas->fill(0, 0, $canvas_bg);
$resized_image = $canvas->merge($image);
However this obviously just adds a white background to the canvas, I cant figure out from their documentation how to make it transparent.
Thanks
How about:
$image = WideImage::loadFromFile($this->local_path);
$resized_image = $image->resizeCanvas(300, 300, 0, 0);
You can specify the X and Y location of the image within the new canvas as well as its width and height. $image->resizeCanvas(300, 300, "center", "center") works quite well.
I have this image cropping application in jquery + php. Jquery sends the co-ordinates to php and php preety much makes thumbnail out of it. The code is below.
$source_image = imagecreatefromjpeg('../../../uploads/'.$dir_name.'/'.$image_name);
$virtual_image = imagecreatetruecolor($width_img, $height_img);
imagecopyresampled($virtual_image, $source_image, 0, 0, $x_img, $y_img, 225, 225, $width_img, $height_img);
imagejpeg($virtual_image, $dest);
Everything is correct in the jquery part. It sends all the co-ordinates correctly. Now the problem arises when I crop the image greater than 225 px in width and 225 pixels in height. Unwanted black colour appears as in the image. The image should have been 225*225.. Only the image part, not the black part.
Example:
ive been using this code and it works fine for my thumbnails
$resource = imagecreatefromjpeg($filepath);
$thumb = imagetruecolor($thumbwidth,$thumbheight);
imagecopyresampled($thumb,$resources,0,0,0,0,$thumbwidth,$thumbheight,$originalWidth,$originalHeight);
I made a small function to upload images then show them on my website.
My question is now, can I change the file size of a image, on same time as I upload it?
Take facebook as example. Even large images has a size on like 65kb.
If so, witch function should I use?
Thanks,
use the imagecopyresampled function:
$source_image = imagecreatefromjpeg("youtimagejpg");
$src_w= imagesx($source_image);
$src_h= imagesy($source_image);
$dest_image = imagecreatetruecolor(100, 100); //targeted width and height
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 100, 100, $source_w, $source_h);
imagejpeg($dest_image,NULL,80);
//Replace null by the path if you want to save on the server, otherwise the image will be sent to the client intead.
If you want to reduce the size of the image without resizing it; use the imagejpeg function (with a low quality factor)
There are next ways to change image size:
Convert to another image format.
Save image with lossy compression (http://en.wikipedia.org/wiki/Lossy_compression)
Resample image with another width & height (http://php.net/manual/en/function.imagecopyresampled.php)
I am trying to create an image which will have my image A (uploaded image) and image B (my watermark image). My problem is that I am not getting the proper way to extend the size of image from the bottom which will be created with image A and B.
My code is -
$img_width=imagesx($img);
$img_height=imagesy($img);
$watermark=imagecreatefrompng($watermark);
$watermark_width=imagesx($watermark);
$watermark_height=imagesy($watermark);
$image=imagecreatetruecolor($img_width, $img_height+35);
imagealphablending($image, false);
$dest_x=$img_width-$watermark_width;
$dest_y=$img_height-$watermark_height+20;
imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0,$watermark_width, $watermark_height);
imagesavealpha($img, true);
imagejpeg($img, $config['pdir']."/t/l-".$thepp, 90);
}
What I am getting with this code is -
Results http://www.9gag.in/pdata/t/l-76.jpg
You can see the watermark image is not totally merged with the image I want to create. I want an extended area in the destination image where the watermark will be fitted properly.
When you writes :
$dest_y=$img_height-$watermark_height+20;
I guess you really want :
$dest_y=$img_height-$watermark_height-20;
Y = 0 at the top of your image, so the more bottom you go, the more high is your height.
What seems to be simple, isn't :(
I'm trying to add something like a watermark (transparent png) on an image (jpg).
This is the code I'm using:
$width = 800;
$height = 600;
$bottom_image = imagecreatefromjpeg("portrait1.jpg");
$top_image = imagecreatefrompng("man2.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height);
header('Content-type: image/png');
imagepng($bottom_image);
When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black.
I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.
You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php
I've searched around the net, I can't find a solution.
Any help is appreciated.
Your $width and $height should be the dimensions of the watermark, not of the photo. What you're telling it to do is copy the watermark with a much bigger size than it is. When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see.
Use imagecopymerge() instead of imagecopy()
U may also like imagesavealpha()