473,695 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="verttes t.jpg";
$fname2="Thumb_ verttest.jpg";
$thumbWidth="30 0";
echo "Creating thumbnail for $fname";
// load image and get image size
$img = imagecreatefrom jpeg($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 = imagecreatetrue color( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresize d( $tmp_img, $img, 0, 0, 0, 0, $new_width,
$new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs }{$fname2}" );
?>

May 3 '07 #1
2 8159
Milagro ha scritto:
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"]["Orientatio n"];
$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) *
May 3 '07 #2
On 2007-05-03 10:45:10 -0400, Mattia Gentilini
<Mattia.Gentili ni_REMOVE_@_REM OVE_CNAF.INFN.I Tsaid:
Milagro ha scritto:
>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"]["Orientatio n"];
$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!

May 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10957
by: Bruce Adams | last post by:
I am using VB6 SP6. I have an application that prints charts. Specifically, it prints line charts of plant process information nightly. The code is a mix of API calls and Printer methods. I am currently migrating to a new PC running XP. The charting application is hanging on a single line of code: Printer.Orientation = vbPRORLandscape It throws an error '484', "Problem getting information from the printer. Make sure printer is set up...
0
5608
by: Mike | last post by:
Sites using thumbnail preview for world wide web file navigation and searching. Below are list of sites that are either researching or providing thumbnail preview images for online web documents. Thumbnail previews are useful for web site navigation particularly in search engines and directories such as Google, Altavista and Yahoo. The preview images provide a portion of the content of the electronic file to aid in navigation.
4
7312
by: tvmaly | last post by:
Does anyone know if it is possible to determine if an image is horizontal/vertical and color or black & white using the python image library? I have been searching this news group and the information was not all clear on this. Best Regards Ty
1
1724
by: Cédric | last post by:
Hello, Wanting to create a thumbnail of an image, i'm using the following piece of code : Public Sub Reduction(ByVal Source As String, ByVal Destination As String, ByVal TailleX As String, ByVal TailleY As String, ByVal Resolution As Integer, ByVal Fond As Boolean, ByVal ConserverRatio As Boolean, ByVal PixelFormat As System.Drawing.Imaging.PixelFormat, ByVal ImageFormat As System.Drawing.Imaging.ImageFormat, ByVal R As Integer, ByVal...
1
3229
by: Xah Lee | last post by:
The following is a program to generate thumbnail images for a website. Useful, if you want to do that. It is used to generate the thumbnails for my “Banners, Damsels, and Mores” project gallery. ( http://xahlee.org/Periodic_dosage_dir/lanci/lanci.html ) Comments and versions in other lang welcome. Xah
8
20361
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1: Start with original thumbnails & two empty sub directories STEP 2: Create smaller versions of the originals for one sub directory STEP 3: Create thumbnail version of the originals the other sub directory STEP 4: Create an index.html pointing to the...
7
2343
by: oscartheduck | last post by:
Hi folks, I'm trying to alter a program I posted about a few days ago. It creates thumbnail images from master images. Nice and simple. To make sure I can match all variations in spelling of jpeg, and different cases, I'm using regular expressions. The code is currently:
8
4691
by: Arno R | last post by:
Hi all. When I need to search for pictures, I always have too choose thumbnail-view manually. Is it possible to open the common dialog in thumbnail-view programmatically? Example ?? At the moment I am using Ken Getz' code. at http://www.mvps.org/access/api/api0001.htm Thanks Arno R
0
8623
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.