PHP creating thumbnails, vertical images changed to horizontal
Greetings...I'm resizing JPGs on the fly by directory. The photos are randomly horizontal or vertical in nature. My script (included below) works as expected - except in one specific case. That case would be when I transfer photos directly from a memory card to the final directory, from which the thumbs are created. In this case - my vertical photos generate horizontal thumbs.
If I process a folder of images that have been processed, resized or not, this problem doesn't happen. The vertical photos are rotated on the camera, on the memory card and this is evidenced by the fact that on my local drive, verticals preview as verticals. However if I run untouched files through the thumbnail script, it rotates verticals 90 degrees clockwise!
Does anyone smart out there have any ideas? :)
Also I toyed with the idea of placing a 'rotate' button in my interface, but I can't get the php function rotateimage() to work. Samples are simle...no errors but in browser I get a mile of symbols and text. Seen that before?
Thanks.
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = ope开发者_运维技巧ndir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new tempopary 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 );
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
Could it be possible that you're passing the height to your function instead of the width in your initial 3 variables? That could create this problem. We don't have the piece of code that passes these variables, you might want to put it up.
精彩评论