Usually, transparent goes black on the questions I found over the Internet. But for me it's the contrary.
I'm trying to split a big image in several ones. It works quite fine but the main problem is that the result can contain black color which is turned to transparent... Please Help ! :)
// I get the image to split and dimensions
$source = #imagecreatefrompng('images/'."full_to_split.png");
$source_width = imagesx( $source );
$source_height = imagesy( $source );
// Setting alpha for transparency
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
// I will split the image vertically by height of 100px
$width = $source_width;
$height = 100;
for( $row = 0; $row < $source_height / $height; $row++)
{
// I get the template image (a "part") which is a 100px height and full width of the source image
$fn_d = sprintf( 'images/'."part_%02d.png", $row+1 );
$destination = imagecreatefrompng($fn_d);
imagecopyresampled( $destination, $source, 0, 0,
0, $row * $height, $width, $height,
$width, $height );
imagepng( $destination, $fn_d );
imagedestroy( $destination );
}
Like this : my parts have transparent backgrounds (so this is perfect) but if there is a (true-)black "spot" in the full image this one is turned to transparent.
If I use the alpha blending on $destination I don't have anymore transparency (it goes black).
I tried with imagecopyresized and imagecopyresampled... It breaks my nerves :p
Thank you
Original colors
Result after split
full_to_split.png
part_01.png
part_02.png
part_03.png
Related
When this code runs the browser screen turns black with a grey and white checkered square in the middle when it is suppose to display a thumbnail within a table cell on a form.
function createThumb( $imageUrl, $thumbWidth )
{
// load image and get image size
$img = imagecreatefromjpeg( $imageUrl );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// display thumbnail
header("Content-type:image/jpeg");
imagejpeg( $tmp_img,$imageUrl,80);
};
And the line that calls the code is
<td >
<?php echo createThumb("CoffeeReg.jpg",75)?>
</td>
I haven't found any of the normal syntax errors, and the code even comes back clean when ran through a syntax checker so that leads me to believe either its a problem with my logic, I'm using the built in functions incorrectly or I have messed up something in the way I am trying to display the new thumbnail only with my lack of exp I don't see where I went wrong. I need to know what I did wrong so I can learn from this mistake.
Okay so after several hours debugging the code that Majid had provided when trying to help me understand where I went wrong with my code I found that the only thing that was needed to make the code he was using work was to add these two lines of code to the end of the secondary php file as shown here...
<?php
$imageUrl = $_GET['file_name'];
$thumbWidth = $_GET['thumb_width'];
// load image and get image size
$mainImage = imagecreatefromjpeg( $imageUrl );
$mainwidth = imagesx( $mainImage );
$mainheight = imagesy( $mainImage );
// calculate thumbnail size
$thumbWidth = intval($mainwidth/4);
$thumbHeight = intval($mainheight/4);;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $thumbWidth, $thumbHeight );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $mainImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainwidth, $mainheight );
// display thumbnail
header("Content-type:image/jpeg");
//Also had to remove the second and third argument from this function
imagejpeg( $tmp_img);
//these following two lines here is what was missing
imagedestroy(tmp_img);
imagedestroy(mainImage);
?>
So I guess that one must clear out the image variables of the thumbnail code or it will break it when it runs. Not sure why removing the two arguments from the imagejpeg function was also needed but it was mentioned in my textbook so i tried it, now with these changes this code works. Thank you Majid for all your help and If your post was still here you would be getting full credit with an up vote and best answer.
I am making thumbnails and for some reason, my output is the correct size, but always black. I saw another Stack Overflow post on a similar topic but in his case, he was passing parameters incorrectly.
I am capturing an image from a video camera and then using this code:
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$url = IMGDIR.$imgname;
$thumburl = IMGDIR."thumb/".$imgname;
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
The result I get is both files saved to the proper directories, both the proper size, but the thumbnail is all black. There must be something simple I am missing. Any ideas?
Remember that PNG files have alpha channels. So be sure to use imagealphablending and imagesavealpha. Here they are integrated into your code.
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
$url = IMGDIR . $imgname;
$thumburl = IMGDIR . "thumb/" . $imgname;
// Set the image alpha blending settings.
imagealphablending($image, false);
imagealphablending($new_image, false);
// Set the image save alpha settings.
imagesavealpha($image, true);
imagesavealpha($new_image, true);
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
Try saving your image's alpha channel with imagesavealpha and passing true for the 2nd argument
imagesavealpha($image, true);
imagepng($image,$url);
imagesavealpha($new_image, true);
imagepng($new_image,$thumburl);
I'm trying to redesign my site so that my original square, tile-based rendering of images can be more of a cutout of the image... to get rid of that grid pattern.
Here's how it looked originally...
Here's a rough mock-up of what I'm going for:
So I resaved an image thumbnail with a transparent background... I just want the dog to show, and the square is transparent which will show the site's background underneath.
Yet when I render it on the page, it has this black background.
I've checked my CSS to see if there is some sort of img class, or class for the rendered comics... or even the bootstrap to see where there may be a background-color being assigned to black (and also searched for hex code 000000), but didn't find one...
Then I found that it had to do with the way my thumbnailing script was resampling the png... Since I'm getting a black background for the supposedly transparent image, I blame imagecreatetruecolor() which returns an image identifier representing a black image of the specified size..
I tried following Cheekysoft's question here about preserving transparency after resampling... but it didn't work... with his two main points being:
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
I found if I put $img = imagecreatefrompng($image_file); before I resize/resample it, it displays transparently... which is what I want... but not after I resample it.
Thumbnailer.php code:
<?php
#Appreciation goes to digifuzz (http://www.digifuzz.net) for help on this
$image_file = $_GET['img']; //takes in full path of image
$MAX_WIDTH = $_GET['mw'];
$MAX_HEIGHT = $_GET['mh'];
global $img;
//Check for image
if(!$image_file || $image_file == "") {
die("NO FILE.");
}
//If no max width, set one
if(!$MAX_WIDTH || $MAX_WIDTH == "") {
$MAX_WIDTH="100";
}
//if no max height, set one
if(!$MAX_HEIGHT || $MAX_HEIGHT == "") {
$MAX_HEIGHT = "100";
}
$img = null;
//create image file from 'img' parameter string
if( preg_match('/\.jpg$/',$image_file) || preg_match('/\.jpeg$/',$image_file) ){
$img = imagecreatefromjpeg($image_file);
} else {
$img = imagecreatefrompng($image_file);
}
//if image successfully loaded...
if($img) {
//get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
//takes min value of these two
$scale = min($MAX_WIDTH/$width, $MAX_HEIGHT/$height);
//if desired new image size is less than original, output new image
if($scale < 1) {
$new_width = floor($scale * $width);
$new_height = floor($scale * $height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//copy and resize old image to new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//replace actual image with new image
$img = $tmp_img;
}
}
//set the content type header
header('Content-Type: image/png', true);
imagealphablending( $img, false );
imagesavealpha( $img, true );
imagepng($img);
imagedestroy($img);
?>
Can anyone help?
Thanks!
You need to call imagealphablending() on your destination image BEFORE you resample/resize the image:
//copy and resize old image to new image
imagealphablending($tmp_img, false);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ...
You placed alphablending and traparent code well below after creating image. Just place them before the your createresample line and transparent image with image that created by imagecreatefrompng.
imagealphablending( $tmp_img, false );
imagesavealpha( $tmp_img, true );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Or you can Try following
$img = ImageCreateFromPNG($image_file);
$tmp_img = imagecreatetruecolor($new_width,$new_height);
imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0));
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
I am creating thumbnails of fixed height and width from my PHP script using the following function
/*creates thumbnail of required dimensions*/
function createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false)
{
/*
* $sourcefilepath = absolute source file path of jpeg
* $destdir = absolute path of destination directory of thumbnail ending with "/"
*/
$thumbWidth = $reqwidth; /*pixels*/
$filename = split("[/\\]",$sourcefilepath);
$filename = $filename[count($filename)-1];
$thumbnail_path = $destdir.$filename;
$image_file = $sourcefilepath;
$img = imagecreatefromjpeg($image_file);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
if($aspectratio==true)
{
$new_height = floor( $height * ( $thumbWidth / $width ) );
}
else
{
$new_height = $reqheight;
}
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
$returnvalue = imagejpeg($tmp_img,$thumbnail_path);
imagedestroy($img);
return $returnvalue;
}
and I call this function with following parameters
createThumbnailofSize($sourcefilepath,$destdir,48,48,false);
but the problem is the resulting image is of very poor quality, when I perform the same operation with Adobe Photo shop, it performs a good conversion.. why it is so? I am unable to find any quality parameter, through which I change the quality of output image..
Use imagecopyresampled() instead of imagecopyresized().
if it is image quality you are after you need to give the quality parameter when you save the image using imagejpeg($tmp_img,$thumbnail_path,100) //default value is 75
/*creates thumbnail of required dimensions*/
function
createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false)
{
/*
* $sourcefilepath = absolute source file path of jpeg
* $destdir = absolute path of destination directory of thumbnail ending with "/"
*/
$thumbWidth = $reqwidth; /*pixels*/
$filename = split("[/\\]",$sourcefilepath);
$filename = $filename[count($filename)-1];
$thumbnail_path = $destdir.$filename;
$image_file = $sourcefilepath;
$img = imagecreatefromjpeg($image_file);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
if($aspectratio==true)
{
$new_height = floor( $height * ( $thumbWidth / $width ) );
}
else
{
$new_height = $reqheight;
}
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
$returnvalue = imagejpeg($tmp_img,$thumbnail_path,100);
imagedestroy($img);
return $returnvalue;
}
You could also consider using ImageMagick (http://us3.php.net/manual/en/book.imagick.php) instead of Gd. I had the same problem just a couple of days ago with Java. Going for ImageMagick instead of Java Advanced Images resultet in a huge quality difference.
tried using the php.Thumbnailer ?
$thumb=new Thumbnailer("photo.jpg");
$thumb->thumbSquare(48)->save("thumb.jpg");
Result photo will be 48x48px. Easy right? :)
You might also want to take a look at the Image_Transform PEAR package. It takes care of a lot of the low-level details for you and makes creating and manipulating images painless. It also lets you use either GD or ImageMagick libraries. I've used it with great success on several projects.
here is the website im talking about
http://makeupbyarpi.com/portfolio.php
you'll notice some of the images are smushed width-wise.
the code i used is this:
$width="500";
$height="636";
$img_src = $_FILES['galleryimg']['tmp_name'];
$thumb = "../gallery/".rand(0,100000).".jpg";
//Create image stream
$image = imagecreatefromjpeg($img_src);
//Gather and store the width and height
list($image_width, $image_height) = getimagesize($img_src);
//Resample/resize the image
$tmp_img = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
//Attempt to save the new thumbnail
if(is_writeable(dirname($thumb))){
imagejpeg($tmp_img, $thumb, 100);
}
//Free memory
imagedestroy($tmp_img);
imagedestroy($image);
the images that get uploaded are huge sometimes 3000px by 2000px and i have php crop it down to 500 x 536 and some landscape based images get smushed. is there a formula i can use to crop it carefully so that the image comes out good?
thanks
You could resize and add a letterbox if required. You simply need to resize the width and then calculate the new height (assuming width to height ratio is same as original) then if the height is not equal to the preferred height you need to draw a black rectangle (cover background) and then centre the image.
You could also do a pillarbox, but then you do the exact same as above except that width becomes height and height becomes width.
Edit: Actually, you resize the one that is the biggest, if width is bigger, you resize that and if height is bigger then you resize that. And depending on which one you resize, your script should either letterbox or pillarbox.
EDIT 2:
<?php
// Define image to resize
$img_src = $_FILES['galleryimg']['tmp_name'];
$thumb = "../gallery/" . rand(0,100000) . ".jpg";
// Define resize width and height
$width = 500;
$height = 636;
// Open image
$img = imagecreatefromjpeg($img_src);
// Store image width and height
list($img_width, $img_height) = getimagesize($img_src);
// Create the new image
$new_img = imagecreatetruecolor($width, $height);
// Calculate stuff and resize image accordingly
if (($width/$img_width) < ($height/$img_height)) {
$new_width = $width;
$new_height = ($width/$img_width) * $img_height;
$new_x = 0;
$new_y = ($height - $new_height) / 2;
} else {
$new_width = ($height/$img_height) * $img_width;
$new_height = $height;
$new_x = ($width - $new_width) / 2;
$new_y = 0;
}
imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height);
// Save thumbnail
if (is_writeable(dirname($thumb))) {
imagejpeg($new_img, $thumb, 100);
}
// Free up resources
imagedestroy($new_img);
imagedestroy($img);
?>
Sorry it took a while, I ran across a small bug in the calculation part which I was unable to fix for like 10 minutes =/ This should work.