Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

resize image problem

Featured Replies

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.

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

why better? timthumb can resample in flight as well.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.