Jump to content

[SOLVED] Advice with image resizing (screen resolution ratio issues)


fireice87

Recommended Posts

iv been resizing images for storage on my site with a simple function but its been bugging me that portraight pictures come out so much bigger than landscape.

i took a screen shot and shoved it into fireworks to check size, the max height on portraight pictures is the same as the max width on landscape when i realised its because of the screen resolution.

 

heres my simple resize algorithym

// get size of image
list($width,$height)=getimagesize($uploadedfile);

// set resize er size
$newwidth=110;
$newheight=($height/$width)*110;
$tmp=imagecreatetruecolor($newwidth,$newheight);

 

what would you do to compensat for this? 110 width is the perfect size so i dont want to change this but 110 is to tall but capping the height down would make it thinner too (unless loosing the ratio)making it to small......... obvously the only choice is a compremise

 

if i wanted to keep my resize code as it is.

  so landscape style images have width set to 110 how would i make

portraight images say be 90 pixels high and keep the ratio.

 

example.jpg

Link to comment
Share on other sites

Don't use hard-coded height or width values or you will stretch your images. To resize to within a height/width range:

 

1. Determine the maximum height and width you want for an image. For instance, say ALL pictures should be no more than 110 pixels wide and no more than 90 pixels tall.

 

2. For each picture, determine whether it is portrait or landscape by comparing it's height to it's width.

 

3. A. For landscape images, find the ratio of the target WIDTH to the actual WIDTH. For instance, if the actual image is 160 pixels, the ratio is 100 pixels / 160 pixels, or 0.625. Multiply both the actual width and the actual height by 0.625 to get the new width and height, then use those values to resize the image.

 

  B. For portrait images, do the same thing, but compare the target HEIGHT to the actual HEIGHT and the change BOTH on the resize.

Link to comment
Share on other sites

ok i was a little confused by that but i think im with you now im not very good with php so before i tackle actually coding it and the unlimited syntax errors that i will create i knocked up some psuedo code to make sure im understanding

 

 

get image size (200 x 150)

 

if width > height

  {

target_width = 110

 

target_width/width = 0.55

 

ratio = 0.55

 

width * ratio = new_width

height * ratio = new_height

  }

else

{

 

      target_height = 90

      target_height/height = 0.6

     

      ratio = 0.6

 

      width * ratio = new_width

      height * ratio = new_height

         

  create(newwidth,newheight)

 

 

 

Im pretty sure this is the idea ?

Link to comment
Share on other sites

And a good idea it was thanks  :)

 

For anyone who finds this thread looking for a solution heres my function to help

 

 

<?php
$uploadedfile = "DisplayPic/$user.jpg";


$src = imagecreatefromjpeg($uploadedfile);

// get size of image
list($width,$height)=getimagesize($uploadedfile);

if ($width > $height)
{
$target_width=110;
$ratio = $target_width/$width;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
}
else
{
$target_height = 110;
$ratio =  $target_height/$height;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;

$tmp=imagecreatetruecolor($newwidth,$newheight);
}
// resize 
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// save resized
$filename = "DisplayPic_Tiny/$user.jpg"; $_FILES['userfile']['name'];
imagejpeg($tmp,$filename,100); 

imagedestroy($src);
imagedestroy($tmp);

?>

 

 

Thanks again for your help

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.