Image resizing on the fly

I’ve found a scipt that resizes images on the fly and outputs to the browser.

Here’s the script:


<?php


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;
   }
}
?>


And here’s my output code:


<?php
	header('Content-Type: image/jpeg');
   include('simpleimage.php');
   $image = new SimpleImage();
   $image->load('http://localhost/foos/siteadmin/images/logo.jpg');
   $image->resizeToWidth(10);
   $image->output();
?>


The only problem is I get this page in my browser…

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php:3) in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php on line 4
ÿØÿà�JFIF������ÿþ�>CREATOR: gd-jpeg v1.0 (using IJG JPEG v70), default quality ÿÛ�C�aaaa    $.’ ",#(7),01444’9=82<.342ÿÛ�C  2!!22222222222222222222222222222222222222222222222222ÿÀ��� "�ÿÄ�����������a ÿÄ�µ���}�!1AQaa"q2‘¡#B±ÁRÑð$3br‚ %&‘()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ��������a ÿÄ�µ�a�w�!1AQaaq"2B‘¡±Á #3RðbrÑ $4á%ñ&’()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ� ��?�õÉY•¥ÚÄ|àð{óÍkÁ5¼LȤ”’:ñErØîÄiýt?ÿÙ

Can anyone help me out with this?

Here’s the PHP info about GD…

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.11
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 7
PNG Support enabled
libPNG Version 1.2.40
WBMP Support enabled
XBM Support enabled
JIS-mapped Japanese Font Support enabled

Does anyone know how to make this work or can anyone offer a good image resizing solution?

Check resize.php (especially line 3) to make sure there’s nothing being output to the browser (not even whitespace).

Sorry. I don’t really understand. Here’s resize.php

<?php
	header('Content-Type: image/jpeg');
   include('simpleimage.php');
   $image = new simpleimage();
   $image->load('http://localhost/foos/siteadmin/images/logo.jpg');
   $image->resizeToWidth(100);
   $image->output();
?>


Now when I run resize.php in my browser I get this:

http://localhost/foos/resize.php

That’s all that’s displayed in the browser. The image path I am using is correct and there’s an image in the correct folder.

We can’t view files on localhost, that’s local to your computer.

Just ensure you have no whitespace or output before the header function.

What Immerse and Ripe are trying to say is that as soon as PHP encounters any white space (new lines, spaces, etc.) or any text at all it sends the headers out, and once it does that, you can’t modify them anymore (hence the error you’re receiving). Make sure that the opening php tag (<?php) is at the very, very beginning of resize.php.

I mean. that URL is exactly what I see in my browser window. I didn’t mean click on it :slight_smile:

This is what I actually see in my browser: http://localhost/foos/resize.php - Shows up in browser as static text. Exact same URL that is in address bar at the top.

Here’s the code as I have it:

<?php
	header('Content-Type:image/jpeg');
   include('http://localhost/foos/simpleimage.php');
   $image = new simpleimage();
   $image->load('/foos/images/blaze.gif');
   $image->resizeToWidth(100);
   $image->output();
?>

I guess you’re using Firefox? if it cannot load an image, it will show the alt text instead. If there is no alt text, it will show the image’s address instead.

That means that there is an error in the file.

Try commenting out $image->output() to see if there’s an error being output before the image is sent to the browser.

Thanks for that.

I have commented that line out. With the line in or out I get this from IE7

Warning: include() [function.include]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php on line 3

Warning: include(http://localhost/foos/simpleimage.php) [function.include]: failed to open stream: no suitable wrapper could be found in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php on line 3

Warning: include() [function.include]: Failed opening ‘http://localhost/foos/simpleimage.php’ for inclusion (include_path=‘.;C:\Documents and Settings\Niall\My Documents\server\xampp\php\PEAR’) in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php on line 3

Fatal error: Class ‘simpleimage’ not found in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\resize.php on line 4

And this from Firefox…

http://localhost/foos/resize.php

Any pointers would be greatly received :slight_smile:

include(‘http://localhost/foos/simpleimage.php’);

Including over HTTP doesn’t make any sense. PHP would try to load the file over HTTP, but it wouldn’t get any PHP code because it would be processed already. Try using a local path instead (e.g. include(‘simpleimage.php’)).

Tried that and get…

Warning: getimagesize(localhost/foos/images/blaze.gif) [function.getimagesize]: failed to open stream: No such file or directory in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 10

Warning: imagesx() expects parameter 1 to be resource, null given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 42

Warning: Division by zero in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 53

Warning: imagesy() expects parameter 1 to be resource, null given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 45

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 63

Warning: imagesx() expects parameter 1 to be resource, null given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 42

Warning: imagesy() expects parameter 1 to be resource, null given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 45

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 64

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\Niall\My Documents\server\xampp\htdocs\foos\simpleimage.php on line 34

What parameter are you calling $image->load() with? Go back to what you had in your third post ($image->load(‘/foos/images/blaze.gif’):wink: and see if that works. Once again, using a URL instead of a local path doesn’t make sense.

If that doesn’t work, make sure you are using the valid path to the image.