Connecting Tech Pros Worldwide Help | Site Map

Thumbnail orientation

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 3rd, 2007, 02:35 PM
Milagro
Guest
 
Posts: n/a
Default Thumbnail orientation

Hi,

I'm using the code below to create thumbnails from photos. The code
works fine and thumbnails are created. However, thumbnails of vertical
photos are oriented as horizontal photos.

More info:
These photos come from a Nikon high end SLR digital camera. It's not my
camera so I have no control over how the image is taken.
If I open a vertical image in Photoshop (CS2), the image is oriented
correctly. When I try to create a thumbnail with php, the thumbnail is
always oriented incorrectly as Horizontal.
I did some further testing and tried opening the same image using
ImageMagic (on a Linux box). ImageMagic also sees the image incorrectly
as Horizontal.

There has to be some way to read the JPG file and determine if it's
meant to be a vertical or horizontal photograph. Are there any php
libraries that are capable of doing this?

Does anyone know the layout of a JPG binary well enough to tell me if
there is a specific flag set for orientation?

Thanks for all of your suggestions.

M



<?

$fname="verttest.jpg";
$fname2="Thumb_verttest.jpg";
$thumbWidth="300";


echo "Creating thumbnail for $fname";


// load image and get image size
$img = imagecreatefromjpeg($fname);
$width = imagesx( $img );
$height = imagesy( $img );


echo "\n";
echo "Current Width:" . $width;
echo "\n";
echo "Current Height:" . $height;
echo "\n";

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

echo "\n";
echo "New Width:" . $new_width;
echo "\n";
echo "New Height:" . $new_height;
echo "\n";

// 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
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname2}" );
?>


  #2  
Old May 3rd, 2007, 02:55 PM
Mattia Gentilini
Guest
 
Posts: n/a
Default Re: Thumbnail orientation

Milagro ha scritto:
Quote:
I'm using the code below to create thumbnails from photos. The code
works fine and thumbnails are created. However, thumbnails of vertical
photos are oriented as horizontal photos.
It seems you have to check the EXIF orientation. EXIF is a metadata
section for JPEG, which most cameras set when they take a photo.
You could access the EXIF data using the exif_read_data() function (see
the doc for info about input parameters) which will return you an array.
After that, you could deterine if the image has to be rotated in this
way ($exif is the array returned by exif_read_data() ):

$o = $exif["IFD0"]["Orientation"];
$rotate = 0;
$flip = false;
switch($o) {
case 1:
$rotate = 0;
$flip = false;
break;

case 2:
$rotate = 0;
$flip = true;
break;

case 3:
$rotate = 180;
$flip = false;
break;

case 4:
$rotate = 180;
$flip = true;
break;

case 5:
$rotate = 90;
$flip = true;
break;

case 6:
$rotate = 90;
$flip = false;
break;

case 7:
$rotate = 270;
$flip = true;
break;

case 8:
$rotate = 270;
$flip = false;
break;
}

Now, $rotate indicates how many degrees the image should be rotated
(clockwise). Then, if $flip is true , you should also flip horizontally
the image (normally this is not the case for digital camera photos)

--
|\/|55: Mattia Gentilini e 55 GigaHertz nel processore
|/_| ETICS project at CNAF, INFN, Bologna, Italy
|\/| www.getfirefox.com www.getthunderbird.com
* Using Mac OS X 10.4.9 powered by Cerebros (Core 2 Duo) *
  #3  
Old May 3rd, 2007, 03:25 PM
Milagro
Guest
 
Posts: n/a
Default Re: Thumbnail orientation

On 2007-05-03 10:45:10 -0400, Mattia Gentilini
<Mattia.Gentilini_REMOVE_@_REMOVE_CNAF.INFN.ITsaid :
Quote:
Milagro ha scritto:
Quote:
>I'm using the code below to create thumbnails from photos. The code
>works fine and thumbnails are created. However, thumbnails of vertical
>photos are oriented as horizontal photos.
It seems you have to check the EXIF orientation. EXIF is a metadata
section for JPEG, which most cameras set when they take a photo.
You could access the EXIF data using the exif_read_data() function (see
the doc for info about input parameters) which will return you an array.
After that, you could deterine if the image has to be rotated in this
way ($exif is the array returned by exif_read_data() ):
>
$o = $exif["IFD0"]["Orientation"];
$rotate = 0;
$flip = false;
switch($o) {
case 1:
$rotate = 0;
$flip = false;
break;
>
case 2:
$rotate = 0;
$flip = true;
break;
>
case 3:
$rotate = 180;
$flip = false;
break;
>
case 4:
$rotate = 180;
$flip = true;
break;
>
case 5:
$rotate = 90;
$flip = true;
break;
>
case 6:
$rotate = 90;
$flip = false;
break;
>
case 7:
$rotate = 270;
$flip = true;
break;
>
case 8:
$rotate = 270;
$flip = false;
break;
}
>
Now, $rotate indicates how many degrees the image should be rotated
(clockwise). Then, if $flip is true , you should also flip horizontally
the image (normally this is not the case for digital camera photos)

Thank you very much, this is exactly what I was looking for.
Ciao!

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.