Thumbnail geration in php is creating unwanted black colour - php

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

Related

imagecopyresampled doesn't give real output

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.

WideImage - Canvas with transparent background

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.

Can i change image file size with php?

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)

Combine two images using PHP GD

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.

Merge images in PHP - GIF and JPG

I am trying to merge two images - a GIF image with a smaller JPG image. The output should be GIF.
The issue is that GIF image colors remain correct, but the colors of the JPG image are altered.
The GIF image has only 256 colors (8-bit), but is there a way to make the merged image to be a true-color resource which later can be converted to a 8-bit GIF for output?
Issue solved.
I updated the code. Here is the solution which works fine:
<?php
header('Content-Type: image/gif');
$gif_address = 'file.gif';
$jpg_address = 'file.jpg';
$image1 = imagecreatefromgif($gif_address);
$image2 = imagecreatefromjpeg($jpg_address);
$merged_image = imagecreatetruecolor(800, 800);
imagecopymerge($merged_image, $image1, 0, 0, 0, 0, 800, 800, 100);
imagecopymerge($merged_image, $image2, 0, 0, 0, 0, 500, 500, 100);
imagegif($merged_image);
imagedestroy($image1);
imagedestroy($image2);
imagedestroy($merged_image);
?>
From your explanation (some code would help), i would hazard a guess you are merging the jpeg onto the gif. Id say the easiest way is to use imageCreateTrueColor to create a new image the size you need then use imagecopy to copy the GIF into this new image. Merge the jpg onto this and then at a later date you can covert the true colour image to a gif.
If im missing something a bit of example code of what you are currently doing might help.

Categories