I'm having some issues uploading PNG images with transparency.
I upload, crop and insert the images in the DB and when I try to show them in the website they come up black. It's only happening with PNG images, for JPG, JPEG and GIF it's working perfect.
Can somebody help me with that?
Thanks in advance!
Example
This is my code:
public function create_square_image($original_file, $destination_file=NULL, $square_size = 100){
// get width and height of original image
$imagedata = getimagesize($original_file);
$original_width = $imagedata[0];
$original_height = $imagedata[1];
if($original_width > $original_height){
$new_height = $square_size;
$new_width = $new_height*($original_width/$original_height);
}
if($original_height > $original_width){
$new_width = $square_size;
$new_height = $new_width*($original_height/$original_width);
}
if($original_height == $original_width){
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
// load the image
if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
$original_image = imagecreatefromjpeg($original_file);
}
if(substr_count(strtolower($original_file), ".gif")){
$original_image = imagecreatefromgif($original_file);
}
if(substr_count(strtolower($original_file), ".png")){
$original_image = imagecreatefrompng($original_file);
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if($new_width>$new_height){
$difference = $new_width-$new_height;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
}
if($new_height>$new_width){
$difference = $new_height-$new_width;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
}
if($new_height == $new_width){
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
// if no destination file was given then display a png
if(!$destination_file){
imagepng($square_image,NULL,9);
}
// save the smaller image FILE if destination file given
if(substr_count(strtolower($destination_file), ".jpg")){
imagejpeg($square_image,$destination_file,100);
}
if(substr_count(strtolower($destination_file), ".gif")){
imagegif($square_image,$destination_file);
}
if(substr_count(strtolower($destination_file), ".png")){
imagepng($square_image,$destination_file,9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
imagedestroy($square_image);
}
You need to use imagesavealpha() in order to preserve the alpha channel when saving.
Related
Original Image:
Desired output: (I added black border for the sack of understanding)
I want to resize image to 200/200 without cropping. See the desired output.
I have this code
<?php
// function created by www.thewebhelp.com
if (!function_exists("create_square_image")) {
function create_square_image($original_file, $destination_file = NULL, $square_size = 96) {
if (isset($destination_file) and $destination_file != NULL) {
if (!is_writable($destination_file)) {
echo '<p style="color:#FF0000">Oops, the destination path is not writable. Make that file or its parent folder wirtable.</p>';
}
}
// get width and height of original image
$imagedata = getimagesize($original_file);
$original_width = $imagedata[0];
$original_height = $imagedata[1];
if ($original_width > $original_height) {
$new_height = $square_size;
$new_width = $new_height * ($original_width / $original_height);
}
if ($original_height > $original_width) {
$new_width = $square_size;
$new_height = $new_width * ($original_height / $original_width);
}
if ($original_height == $original_width) {
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
// load the image
if (substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")) {
$original_image = imagecreatefromjpeg($original_file);
}
if (substr_count(strtolower($original_file), ".gif")) {
$original_image = imagecreatefromgif($original_file);
}
if (substr_count(strtolower($original_file), ".png")) {
$original_image = imagecreatefrompng($original_file);
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if ($new_width > $new_height) {
$difference = $new_width - $new_height;
$half_difference = round($difference / 2);
imagecopyresampled($square_image, $smaller_image, 0 - $half_difference + 1, 0, 0, 0, $square_size + $difference, $square_size, $new_width, $new_height);
}
if ($new_height > $new_width) {
$difference = $new_height - $new_width;
$half_difference = round($difference / 2);
imagecopyresampled($square_image, $smaller_image, 0, 0 - $half_difference + 1, 0, 0, $square_size, $square_size + $difference, $new_width, $new_height);
}
if ($new_height == $new_width) {
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
// if no destination file was given then display a png
if (!$destination_file) {
imagepng($square_image, NULL, 9);
}
// save the smaller image FILE if destination file given
if (substr_count(strtolower($destination_file), ".jpg")) {
imagejpeg($square_image, $destination_file, 100);
}
if (substr_count(strtolower($destination_file), ".gif")) {
imagegif($square_image, $destination_file);
}
if (substr_count(strtolower($destination_file), ".png")) {
imagepng($square_image, $destination_file, 9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
imagedestroy($square_image);
}
}
create_square_image("image.jpg", "sample_thumb.jpg", 200);
?>
<h2>Original image</h2>
<h2><img src="image.jpg" />
</h2>
<h2>The created square thumbnail</h2>
<h2><img src="sample_thumb.jpg" />
</h2>
But my code is outputting like this
How about using a white image that matches your dimensions? I.e you get a white image that is your desired size (200x200) and merge it with your other image.
// First image is the white image that has the dimension you want.
$image1 = imagecreatefrompng('COLOR.png');
// Second image is the image you want to change size of
$image2 = imagecreatefrompng('SOURCE.png');
// Merge the two, so that the white image is below the "Dress"
// image you showed us in your question.
imagecopymerge($image1, $image2, 0, 0, 0, 0, 161, 200, 100);
// Output and free from memory
header('Content-Type: image/png');
echo imagepng($image1);
// Destroy images
imagedestroy($image1);
imagedestroy($image2);
Parameters for the function:
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 )
Last parameter is percentage(alpha) so if you don't want a white backround then you just set it to 0, and it will be transparent.
EDIT: Check out the docs so that you get the correct position for the second image here.
EDIT 2: The above code will produce the following image(provided the color image is black):
I have found the solution that works for both situation whether image is wide or tall.
$whereToPut = "source.jpg"; // This is 200/200 blank white image
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
$dst_y = 0;
$dst_x = 0;
if ($ratio > 1) {
$width = 200;
$height = 200 / $ratio;
$dst_y = (200 - $height) / 2;
} else {
$width = 200 * $ratio;
$height = 200;
$dst_x = (200 - $width) / 2;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
$image1 = imagecreatefromjpeg($whereToPut);
imagecopymerge($image1, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
imagejpeg($image1, $finalImage);
// $dinalImage is your final created image.
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
Hi friends i want create thumbnail from image url in php, am finding one code to generate thumb image from url ,it's working for low memory size image url, NOT WORKING FOR High Memory size image url, please give some solution for this problem.
<?php
// function created by www.thewebhelp.com
if(!function_exists("create_square_image")){
function create_square_image($original_file, $destination_file=thumb, $square_size = 96){
if(isset($destination_file) and $destination_file!=NULL){
if(!is_writable($destination_file)){
echo '<p style="color:#FF0000">Oops, the destination path is not writable. Make that file or its parent folder wirtable.</p>';
}
}
// get width and height of original image
$imagedata = getimagesize($original_file);
$original_width = $imagedata[0];
$original_height = $imagedata[1];
if($original_width > $original_height){
$new_height = $square_size;
$new_width = $new_height*($original_width/$original_height);
}
if($original_height > $original_width){
$new_width = $square_size;
$new_height = $new_width*($original_height/$original_width);
}
if($original_height == $original_width){
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
// load the image
if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
$original_image = imagecreatefromjpeg($original_file);
}
if(substr_count(strtolower($original_file), ".gif")){
$original_image = imagecreatefromgif($original_file);
}
if(substr_count(strtolower($original_file), ".png")){
$original_image = imagecreatefrompng($original_file);
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if($new_width>$new_height){
$difference = $new_width-$new_height;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
}
if($new_height>$new_width){
$difference = $new_height-$new_width;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
}
if($new_height == $new_width){
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
// if no destination file was given then display a png
if(!$destination_file){
imagepng($square_image,NULL,9);
}
// save the smaller image FILE if destination file given
if(substr_count(strtolower($destination_file), ".jpg")){
imagejpeg($square_image,$destination_file,100);
}
if(substr_count(strtolower($destination_file), ".gif")){
imagegif($square_image,$destination_file);
}
if(substr_count(strtolower($destination_file), ".png")){
imagepng($square_image,$destination_file,9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
imagedestroy($square_image);
}
}
create_square_image("imageurl","thumbname.jpg",200);
?>
Guys i know that this question is asked before but its not the same because i have tried others answers and nothing worked as I want.
I want to create thumbnail image using php and the thumbnail width="265px" and height="125px" and here is my code :
$image_width = $imageSize[0];
$image_height = $imageSize[1];
$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 45)); //this will set any image to 125*70 which is a good thumbnail
$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;
if($ext == "gif"){
$old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){
$old_image = imagecreatefrompng($real_image_path);
}else{
$old_image = imagecreatefromjpeg($real_image_path);
}
$new_image = imagecreatetruecolor($new_width, $new_height); //creating new image with a new color for quality
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
I am not very sure about my comments but i just wrote them just in case
I think it need someone that is good in maths
Thanks and happy new year
No math required..
if($ext == "gif"){
$old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){
$old_image = imagecreatefrompng($real_image_path);
}else{
$old_image = imagecreatefromjpeg($real_image_path);
}
$data = getimagesize($real_image_path);
$height = 125;
$width = 265;
$thumb = imagecreatetruecolor($width, $height);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb, $old_image, 0, 0, 0, 0, $width, $height, $data[0], $data[1]);
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.