Jump to content

Image re-sizing


gerkintrigg

Recommended Posts

Okay, so I'm not too hot on the GD library, but kinda understand the principles.

I just wondered what the easiest way is of re-sizing images so that the maximum dimentions of them are set sizes...

so if i want something no bigger than 400 x 500, then no dimention would be bigger than those specified, but it'd keep whatever proportions it had before...

I hope that makes sense.

Please help.

For those interested in code... I have the following right now, but it just makes every image specific dimentions and does not maintain proportions:

[code]$thumb_w=100;
$thumb_h=17;

function makeimage($filename,$newfilename,$path,$newwidth,$newheight) {
$img_path = strstr($filename, 'n_code');

//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
$image_type = strstr($img_path, '.');

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
case '.jpg':
$source = imagecreatefromjpeg('uploaded_images/'.$img_path);
break;
case '.png':
$source = imagecreatefrompng('uploaded_images/'.$img_path);
break;
case '.gif':
$source = imagecreatefromgif('uploaded_images/'.$img_path);
break;
default:
echo("Error Invalid Image Type");
die;
break;
}

//CREATES THE NAME OF THE SAVED FILE
$file = $newfilename . $img_path;

//CREATES THE PATH TO THE SAVED FILE
$fullpath = 'uploaded_images/'.$path . $file;

//FINDS SIZE OF THE OLD FILE
list($width, $height) = getimagesize('uploaded_images/'.$img_path);

//CREATES IMAGE WITH NEW SIZES
$thumb = imagecreatetruecolor($newwidth, $newheight);

//RESIZES OLD IMAGE TO NEW SIZES
if (($height > $thumb_h) or ($width > $thumb_w)) {
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}

//SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
imagejpeg($thumb, $fullpath, 100);

//CREATING FILENAME TO WRITE TO DATABSE
$filepath = $fullpath;

//RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
return $filepath;
}
echo(makeimage('n_code10.jpg','','imgs/',$thumb_w,$thumb_h));
[/code]
Link to comment
https://forums.phpfreaks.com/topic/32714-image-re-sizing/
Share on other sites

I just did this. here is the function I use
[code]<?php
header('Content-type: image/jpeg');
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $imgcomp=0)
  {
  $g_imgcomp=100-$imgcomp;
  $g_srcfile=$sourcefile;
  $g_fw=$forcedwidth;
  $g_fh=$forcedheight;

  if(file_exists($g_srcfile))
      {
      $g_is=getimagesize($g_srcfile);
      if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
          {
          $g_iw=$g_fw;
          $g_ih=($g_fw/$g_is[0])*$g_is[1];
          }
          else
          {
          $g_ih=$g_fh;
          $g_iw=($g_ih/$g_is[1])*$g_is[0];
          }
      $img_src=imagecreatefromjpeg($g_srcfile);
      $img_dst=imagecreatetruecolor($g_iw,$g_ih);
      imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
      imagejpeg($img_dst,'',90);
      imagedestroy($img_dst);
      imagedestroy($img_src);
      return true;
      }
      else
      return false;
  }
resampimagejpg("200", "200", "images/TEST.jpg");
?>[/code]

If you are using this to resize images from queries in a database let me know. Also you can only show one image at a time unless you call this function from another page.


Ray
Link to comment
https://forums.phpfreaks.com/topic/32714-image-re-sizing/#findComment-152273
Share on other sites

The one I have have posted will keep it proportional it will check to see which is greater, width or height, then resize based on the larger of the 2. So you could say I want the width to be no more than 300 and the height to be no more than 200 and it will resize with the larger of the 2 and do it proportionally. So if the width falls at 300 and the height is 180 then it will use the width parameter but if the width is 200 and the height is 200 it will use the height parameter of 200.

If you are going to be pulling multiple images out of the table then you cannot use the function on the same page as the sql script. You have to call the function script from the table returns.

Example I have 2 files.
imageresize.php
[code]<?php
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $imgcomp=0)
  {
  $g_imgcomp=100-$imgcomp;
  $g_srcfile=$sourcefile;
  $g_fw=$forcedwidth;
  $g_fh=$forcedheight;

  if(file_exists($g_srcfile))
      {
      $g_is=getimagesize($g_srcfile);
      if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
          {
          $g_iw=$g_fw;
          $g_ih=($g_fw/$g_is[0])*$g_is[1];
          }
          else
          {
          $g_ih=$g_fh;
          $g_iw=($g_ih/$g_is[1])*$g_is[0];
          }
      $img_src=imagecreatefromjpeg($g_srcfile);
      $img_dst=imagecreatetruecolor($g_iw,$g_ih);
      imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
      imagejpeg($img_dst,'',90);
      imagedestroy($img_dst);
      imagedestroy($img_src);
      return true;
      }
      else
      return false;
  }
resampimagejpg($_GET['width'], $_GET['height'], $_GET['source']);
>?[/code]

and here is my script for the sql
imagecall.php
[code]<?php
header('Content-type: image/jpeg');
require('config.php');
include('includes/mysql.php');
$sql = 'SELECT Item_id,Item_pic,Item_desc, Unit_price FROM Item ORDER BY Item_id';
$result = mysql_query($sql);
echo '<table border="3"><tr><td>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$pic = "./images/".$row['Item_pic'];
  echo "<tr><td>\n";
  echo "<img src='imageresize.php?width=200&height=200&source=$pic'>";
  echo "</td> <td>".$row['Item_desc']."&nbsp;<td>Price: RM".$row['Unit_price']."&nbsp;<td><a href=\"cart.php?action=add&id=".$row['Item_id']."\">Add to cart</td></tr>";
}
echo '</td></tr></table>';
?>[/code]

Ray
Link to comment
https://forums.phpfreaks.com/topic/32714-image-re-sizing/#findComment-152296
Share on other sites

I apologize!! my function was not sizing proportionately. I t was only looking at the width. Here is a revamped function to do what you want.
[code]<?php
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $imgcomp=0)
  {
  $g_imgcomp=100-$imgcomp;
  $g_srcfile=$sourcefile;
  $g_fw=$forcedwidth;
  $g_fh=$forcedheight;

  if(file_exists($g_srcfile))
      {
      $g_is=getimagesize($g_srcfile);
      $w_adjust = ($g_fw / $g_is[0]);
      $h_adjust = ($g_fh / $g_is[1]);
      if($w_adjust <= $h_adjust)
          {
          $g_iw=($g_is[0]*$w_adjust);
          $g_ih=($g_is[1]*$w_adjust);
          }
          else
          {
          $g_iw=($g_is[0]*$h_adjust);
          $g_ih=($g_is[1]*$h_adjust);
          }
      //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
    $image_type = strrchr($sourcefile, ".");

    //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
case '.jpg':
$img_src = imagecreatefromjpeg($g_srcfile);
break;
case '.png':
$img_src = imagecreatefrompng($g_srcfile);
break;
case '.gif':
$img_src = imagecreatefromgif($g_srcfile);
break;
default:
echo("Error Invalid Image Type");
die;
break;
}
      $img_dst=imagecreatetruecolor($g_iw,$g_ih);
      imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
      imagejpeg($img_dst,'',90);
      imagedestroy($img_dst);
      imagedestroy($img_src);
      return true;
      }
      else
      return false;
  }
resampimagejpg($_GET['width'], $_GET['height'], $_GET['source']);
?>[/code]

Ray
Link to comment
https://forums.phpfreaks.com/topic/32714-image-re-sizing/#findComment-152318
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.