Jump to content

Image aspect ratio ?


EchoFool

Recommended Posts

Is there such a function which allows you pass the image url into a function, it then resizes the image keeping its aspect ratio so that people don't look fat or thin when the picture is in thumbnail form ?

 

Can't see to do it in standard CSS so im wondering if php can some how do it =/ ?

Link to comment
Share on other sites

I think everyone who uses images has written at least one thumbnail function. Here's one that I did:

<?php
/*
*  $fn  -- input file
*  $shrink -- width to shrink picture to if in landscape, height if in portait
*  $rt -- return image to calling script if "true", send to display if "false"
*/
function makethumb($fn,$shrink,$rt=true)
{
	list($ow, $oh, $type, $attr) = getimagesize($fn);
	if ($oh > $ow) {
		$th = $shrink;
		$tw = ($th / $oh) * $ow;
	}
	if ($ow >= $oh) {
		$tw = $shrink;
		$th = ($tw / $ow) * $oh;
	}
	$w = round($tw);
	$h = round($th);
	$tn = imagecreatetruecolor($w, $h);
	$img = imagecreatefromjpeg($fn);
	imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh);
	if ($rt) {
		return (array($tn, $type, $w, $h));
	} else {
   		        header('Content-type: ' .image_type_to_mime_type($type));
		imagejpeg($tn);
	}
}
?>

 

Ken

Link to comment
Share on other sites

how are you actually using it on your site? Sounds like you are calling the function and then echoing out what it returns (leaving that last  argument true).  If that's what you are doing, you need to send the proper headers first so that the browser knows its an image to be rendered. Otherwise, the browser is just printing out the raw data of the image.  Look at the function, at the code when $rt is set to false.

Link to comment
Share on other sites

makethumb($Url,260);

This send image header and  output the image directly.  You don't really want to do this unless the only thing being output from your script is the image.

 

$image = makethumb($Url,260,false);

 

This will make the change to the image and return the altered image resource.  You will want to do it like this if you are wanting to do something more with the image and then output it later, or save it to disk instead or whatever.

Link to comment
Share on other sites

I tried both methods - but get this in both circumstances :

 

Before the first part i get:

 

 

Warning:  Cannot modify header information - headers already sent by (output started at main.php:85) in functions.php on line 548

 

 

Obviously its unreadable :P

 

http://www.paste.to/ODA4Mw==  im assuming this is raw image data?

 

<?php
function makethumb($fn,$shrink,$rt=false)
   {
      list($ow, $oh, $type, $attr) = getimagesize($fn);
      if ($oh > $ow) {
         $th = $shrink;
         $tw = ($th / $oh) * $ow;
      }
      if ($ow >= $oh) {
         $tw = $shrink;
         $th = ($tw / $ow) * $oh;
      }
      $w = round($tw);
      $h = round($th);
      $tn = imagecreatetruecolor($w, $h);
      $img = imagecreatefromjpeg($fn);
      imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh);
      if ($rt) {
         return (array($tn, $type, $w, $h));
      } else {
                 header('Content-type: ' .image_type_to_mime_type($type));
         imagejpeg($tn);
      }
   }

$Url = 'http://www.google.co.uk/images/logos/ps_logo2.png';  //test image off the net
$image = makethumb($Url,260,false);
echo $image;
?>

Link to comment
Share on other sites

also, you can't just echo $image.  It's an image source object.  If you want to output it to browser, you have to do it like this:

 

header('Content-type: ' .image_type_to_mime_type($type));
imagejpeg($tn); // for jpg files, there's other image type equivalents to this

 

 

would image.$type($tn); work or is the functions not named equal to the file type? With the header line its always going to error if its the middle of script and i can't put the header else where it won't know the $type without the variable which is half way through the script so theres no logic to it.

Link to comment
Share on other sites

Well they are but...you can't just call a function like that....if you really want to do it like that then you would have to eval it, but that's not really a good idea.  Instead, you should setup a condition to output based on type of image.

 

$ext = strtolower(pathinfo($Url, PATHINFO_EXTENSION));
switch($ext) {
  case  'jpg' :
  case 'jpeg' : imagejpeg($tn); break;
  case  'png' :  imagepng($tn); break;
  case  'gif' :  imagegif($tn); break;
  case  'bmp' : imagewbmp($tn); break;
}

Link to comment
Share on other sites

With the header line its always going to error if its the middle of script and i can't put the header else where it won't know the $type without the variable which is half way through the script so theres no logic to it.

 

You need to

 

a) save the image instead of output and then point to it like any other image (in an <img .../> tag)

 

b) seeing as how option "a" isn't all that great, an alternative is to make the image generation script a completely separate script all by itself.  You would then output your image tag in your script as you normally output an image tag, except point to the image script like so:

<img src='image.php?url=urlhere&shrink=260' ... />

 

 

Then in script.php is where you have that function and the only thing it will do is the arguments from $_GET['url'] and $_GET['shrink'] and then output the image.

Link to comment
Share on other sites

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.