Page 1 of 1

Rezising images proporcianally

Posted: Wed May 16, 2007 3:27 pm
by franknu
ok i want to resize the images proporcionally here is my code

Code: Select all

$image2 = "$Pictures2";
$size = getimagesize($image2);

$scale = $size[1]/$size[0];
$width2 = 250;
$height2 = $width * $scale;
here is my error display,

Warning: Division by zero file online 68

it display the image to thin too i can hardly be seen.
it any idea please

Re: Rezising images proporcianally

Posted: Wed May 16, 2007 5:06 pm
by califdon
franknu wrote:ok i want to resize the images proporcionally here is my code

Code: Select all

$image2 = "$Pictures2";
$size = getimagesize($image2);

$scale = $size[1]/$size[0];
$width2 = 250;
$height2 = $width * $scale;
here is my error display,

Warning: Division by zero file online 68

it display the image to thin too i can hardly be seen.
it any idea please
It would appear that your getimagesize() function isn't returning what you expect it to. Have you echoed the values of $size[0] and $size[1]?

Posted: Thu May 17, 2007 10:55 pm
by Jaxolotl
This code may help you, I create this function time ago to have the proportional height when resizing width. Hope it will be usefull for you.
Anyway the Division by 0 error seems to be that you are passing a wrong file path to getimagesize() function, using the @ before as on my function elliminates only the error mese but not the error itself.
From manual http://ar.php.net/manual/it/function.getimagesize.php
If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will return FALSE and generate an error of level E_WARNING.
So in simple words
$size= FALSE ( from your code $size = getimagesize($image2))
$size['0'] = 0
and
$size['1'] = 0
what means
$scale = 0/0 then the error message

Code: Select all

<?php

// THE FUNCTION
function adapt_thumbnail_height($width, $height , $custom_width){
  /*
  formula is PROPORTION = (H/100) * ((CustomW/W)*100)
  */

  /*
  TRY THIS IF YOU WANT TO RESIZE IMAGES ONLY WHEN LARGER THAN WANTED
  
  if(($width > 0)&&($width < $custom_width)){
    $custom_width = $width ;
  }
  */
  
  return @round(($height / 100) * (($custom_width/$width)*100));
  
  // I choose round() because dimensions in pixels are integers 
  // and it should return the nearest integer of the division result
  // to be as similar as possible to the original proportion
}


// SOME CODING
$imageXY = @getimagesize("ImagePath");

$width = (($imageXY) ? ($imageXY['0']) : "");
$height = (($imageXY) ? ($imageXY['1']) : "");

$new_width = 200;
$new_height = adapt_thumbnail_height($width, $height , $new_width);

 echo ($width/$height) . " | " . ($new_width/$new_height); // see propportions
?>

Posted: Fri May 18, 2007 5:36 am
by onion2k
Two things..

1. There's a lovely thumbnail class that maintains proportions on http://www.phpgd.com ... There's ones that do fancy stuff like rounding corners and drop shadows too.

2. When you use getimagesize() you should always wrap it in a block that checks the image exists using file_exists(). Never assume a file exists even if you're sure it always will.

Posted: Fri May 18, 2007 9:17 am
by Jaxolotl
onion2k wrote: 2. When you use getimagesize() you should always wrap it in a block that checks the image exists using file_exists(). Never assume a file exists even if you're sure it always will.
This is always a good point and a good programming procedure "validating the information or resource you are going to work to. Because of that I use to use these two functions

Code: Select all

############################################### IS IMAGE? file exists?
function is_image($string){
  // check if file exists and if at the end of the string there is an image extension
  return ((file_exists("$string"))&&(preg_match('#(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$#i',$string)))  ? true : false ;
}
############################################### return an alternative when the given one is missing
function image_exists($string,$default){
  // $string is the image you are passing, default is the alternative image string
  return (is_image($string)) ? ("$string") : ("$default");
}


/*
So you coud use:
$my_image = image_exists("images/image1.jpg","images/default.jpg";

$imageXY = getimagesize($my_image);
*/
Of course the image control is just "filename based" so it doesn't check if it's really an image file, you could be less permissive if you need. (permissive??? is it correct in english?)

onion2k wrote: 1. There's a lovely thumbnail class that maintains proportions on http://www.phpgd.com ... There's ones that do fancy stuff like rounding corners and drop shadows too.
Hummm could be a nice point to discuss!!! Of course if you are on a productive team or need to resolve something in a quick efficient way your point would be perfect form me, meaning "don't create the hot water again if someone does it before an better" but..... I think that you can only become an excellent programmer (if you can of course; I think it'll takes me 2 or 3 lives to be there....may be more ;) ) is writing your own code and use the foreign scripts just for inspiration or understanding procedure and logic, the human being learns only when he/she needs to find a solution, to adapt. And of course creating your own code is the only way to have the solutions that fits with your problem (not more, not less). It's not good to use a Jedi laser sword to cut your nails when you can use scissors (may be this is not the case because the script you show us is very beautyfull and fast). Anytime I can I take advantages of creating the hot water again just to see what comes out, sometimes creating the hot water you have the answer for something else unexpected.

Posted: Fri May 18, 2007 11:36 am
by Grim...
onion2k wrote:2. When you use getimagesize() you should always wrap it in a block that checks the image exists using file_exists(). Never assume a file exists even if you're sure it always will.
Why not just check that getimagesize() doesn't return false?

Code: Select all

if (getimagesize($myimagepath)) {
    ...
} else {
    print "Image not found!";
}
?

Posted: Fri May 18, 2007 11:40 am
by onion2k
That's a different check. You're checking whether or not getimagesize() worked on the supplied filename. There could be many reasons for it to fail, the file not being a valid image for example. I test for a file's existence prior to doing anything with it.

Posted: Fri May 18, 2007 12:44 pm
by feyd
Something of note: a file's extension has nothing to do with the content of said file.