Jump to content

Recommended Posts

please avoid double-posting your topics. to resize an image, you'll need to use some of the GD functions. keeping the same aspect ratio is a simple calculation, and here's an example tutorial:

 

http://www.phptoys.com/e107_plugins/content/content.php?content.46

 

there's a plethora of these tutorials in google, i'd suggest looking around.

 

another option is using a third-party plugin such as phpThumb, which is extremely handy and fairly quick to setup.

http://us2.php.net/imagecopyresampled

 

The new dimensions though you will have to calculate. The easiest way is to take your aspect ratio:

320 / 200 = 1.6

Then on the new image, if it's aspect ratio is greater then 1.6 you will want to resize the height and crop the width. Less then 1.6, resize the width and crop the height. aka:

 

Starting Image: 600 x 300
600 / 300 = 2.0 ~~~> Greater then 1.6, so height resize, width crop
300 / 200 * 600 = 400 ~~~> Resized Height (resized width is 200)
Resized Image: 400 x 200
Crop Image to 320 x 200

please avoid double-posting your topics. to resize an image, you'll need to use some of the GD functions. keeping the same aspect ratio is a simple calculation, and here's an example tutorial:

 

http://www.phptoys.com/e107_plugins/content/content.php?content.46

 

there's a plethora of these tutorials in google, i'd suggest looking around.

 

another option is using a third-party plugin such as phpThumb, which is extremely handy and fairly quick to setup.

 

 

Double posting? Uhm I posted a topic of this in the CSS board, but since it became a PHP query, I posted it in the PHP board?..

 

Thanks guys, will check the two links!

Creates perfect resized images:

 


class SimpleImage {
   
   var $image;
   var $image_type;

   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}

//******************settings below*****************//
// resize
$image = new SimpleImage();
$image->load(pic.jpg);
$image->resize(140,110); //wxh
$image->save(pics/pic.jpg);

Thank you akitchin, rhodesa and dreamwest!

 

I struggled allot with it but I finally got it lol:

 

If anyone is interested:

<?php
function image_resize($original_image,$to_width,$to_height)
{// Get the original geometry and calculate scales
    list($width, $height) = getimagesize($original_image); 
    $xscale=$width/$to_width;
    $yscale=$height/$to_height;

    // 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));
     }
    global $new_image;
    $new_image = imagecreatetruecolor($new_width, $new_height);
    $tmp_image = imagecreatefromjpeg ($original_image);
    imagecopyresampled($new_image, $tmp_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);	
}
?>

$file_tmp = $_FILES[pic][tmp_name];

if (is_uploaded_file($file_tmp))
{image_resize($file_tmp, 320, 260);
  imagejpeg($new_image, "../file_uploads/userimage.jpeg");
}

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.