Web Design Forum: resize image problem - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

resize image problem Rate Topic: -----

#1 User is offline   adsegzy 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 55
  • Joined: 15-February 10
  • Reputation: 1

Posted 28 August 2010 - 04:17 PM

Hello friends,

please am having problem with my image resize code the code is as below;


$originalImage = $_FILES["file"]["name"];
$toWidth = 300;
$toHeight = 300;

function resizeImage($originalImage,$toWidth,$toHeight){

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}

// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

return $imageResized;
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $imageResized);
$path2= "../upload/" . $imageResized;


but the picture is not resized, and if i use


$image = resizeImage()
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $image);
$path2= "../upload/" . $image;


no picture will be uploaded to my upload folder.

please what can i do or which other lesser stress way can i resize my picture.
0

#2 User is offline   Tangled Frog 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 17-August 10
  • Reputation: 1
  • Gender:Female
  • Location:Edinburgh
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 30 August 2010 - 10:29 AM

Hi there,

You might like to try using something like this instead, it should do the trick if you're using PHP 5:

 $originalImage = $_FILES['image']['tmp_name'];
 $src = imagecreatefromjpeg($originalImage);
 list($width,$height) = getimagesize($originalImage);
 $ratio = $width/$height;
 
 if ($width > $height)
 {if ($width > 300)
  {$new_height = 300 / $ratio;
   $new_width =  300;}
  else
  {$new_height = $height;
   $new_width =  $width;}}
 else
 {if ($height > 300)
  {$new_width = 300 * $ratio;
   $new_height =  300;}
  else
  {$new_height = $height;
   $new_width =  $width;}}
  
 $target = "upload/image.jpg";
 $tmp = imagecreatetruecolor($new_width,$new_height);
 imagecopyresampled($tmp,$src,0,0,0,0,$new_width,$new_height,$width,$height);
 imagejpeg($tmp,$target,100);



This will resize your image to a maximum of 300px while retaining the original dimensions.

If you're still having problems the manual for GD library is very helpful and not too hard to understand.

Hope it works out ok!

Kirsty
1

#3 User is online   zed 

  • Web Guru
  • Group: Moderators
  • Posts: 4,677
  • Joined: 25-May 10
  • Reputation: 673
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 31 August 2010 - 06:21 AM

or use timthumb? http://www.binarymoo...jects/timthumb/

works a treat.
1

#4 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,503
  • Joined: 03-January 10
  • Reputation: 247
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 01 September 2010 - 09:24 AM

Or even better use http://shiftingpixel...-image-resizer/ - really simple to use and has tons of features.
1

#5 User is online   zed 

  • Web Guru
  • Group: Moderators
  • Posts: 4,677
  • Joined: 25-May 10
  • Reputation: 673
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 01 September 2010 - 09:27 AM

why better? timthumb can resample in flight as well.
0

#6 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,503
  • Joined: 03-January 10
  • Reputation: 247
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 03 September 2010 - 09:38 AM

View Postzed, on 01 September 2010 - 09:27 AM, said:

why better? timthumb can resample in flight as well.


Calm down, was just saying :)

I always used the shifting pixel one I found it to be very lightweight and have all the features I need.

Although one extra feature that one has (that I probs wouldn't need) is the ability to specify a crop location and a filter, which is kinda nice. I'm sure they do the same job in the end.

Be happy, it's Friday and England are playing tonight :)

EDIT: one thing to remember is that all image reizers like this generate really messy urls in your webpage. I'd advise using some url rewriting to change something like
timthumb.php?src=lighthouse.jpg&a=r&w=60&h=130&f=3,80 into soemthing like
/images/thumbs/lighthouse.jpg



0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users