New version of code, now delivers a blank image if its called with incorrect image paramiters.
Also errors messages are now suppressed.
<?php error_reporting(0);if( ! defined( 'IMG_IP' ) ) { define( 'IMG_IP', 0 ); } // Image interpolation if( ! defined( 'IMG_MAXSIZE' ) ) { define( 'IMG_MAXSIZE', 9999 ); } // can't be any largerclass Image{ var $data; var $filename; var $img; function Image( $mixed = 0 ) { if( empty( $mixed ) ) { return $this; } if( ! $this->ImageFromFile( $mixed ) ) { if( ! $this->ImageFromData( $mixed ) ) { return( 0 ); } } return $this; } function ImageFromFile( $f ) { if( ( $fp = fopen( $f, 'rb' ) ) || ($fp = fopen($_SERVER['DOCUMENT_ROOT'] . $f, 'rb')) ) { $this->data = fread( $fp, ( filesize( $f ) ? filesize( $f ) : IMG_MAXSIZE ) ); $this->filename = $f; return $this->ImageFromData( $this->data ); } else { return( 0 ); } return( 1 ); } function ImageFromData( $d ) { if( $this->img = ImageCreateFromString( $d ) ) { return( 1 ); } return( 0 ); } function save( $filename = '', $print = 0 ) { if( empty( $filename ) ) { if( empty( $this->filename ) ) { return 0; } else { $filename = $this->filename; } } //print $filename; $filetype = substr( strrchr( $filename, '.' ), 1 ); switch( $filetype ) { case 'jpg': case 'jpeg': if( ImageTypes() & IMG_JPG ) { if( $print ) { header( "Content-type: image/jpeg\n\n" ); return ImageJPEG( $this->img, null, 60 ); } else return ImageJPEG( $this->img, $filename, 60 ); } break; case 'png': if( ImageTypes() & IMG_PNG ) { if( $print ) { header( "Content-type: image/png\n\n" ); return ImagePNG( $this->img ); } else return ImagePNG( $this->img, $filename ); } break; case 'gif': if( ImageTypes() & IMG_GIF ) { if( $print ) { header( "Content-type: image/gif\n\n" ); return ImageGIF( $this->img ); } else return ImageGIF( $this->img, $filename ); } break; case 'bmp': if( ImageTypes() & IMG_WBMP ) { if( $print ) { header( "Content-type: image/wbmp\n\n" ); return ImageWBMP( $this->img ); } else return ImageWBMP( $this->img, $filename ); } break; } } function p() { return $this->save( 0, 1 ); } function scale_image( $newWidth = 0, $newHeight = 0, $ratio = 0, $copy = '' ) { if( ! empty( $copy ) ) { global $$copy; } $width = imageSX( $this->img ); $height = imageSY( $this->img ); if( empty( $newWidth ) && empty( $newHeight ) ) { // no dimensions, check ratio if( empty( $ratio ) ) { // nothing to scale to, return return( 0 ); } else { // get dimensions from ratio $dimensions[] = round( $width * $ratio ); $dimensions[] = round( $height * $ratio ); } } else if( empty( $newWidth ) || empty( $newHeight ) ) { // we have one dimension, use this as the max and run through ratio() $dimensions = $this->ratio( ( ! empty( $newWidth ) ? $newWidth : $newHeight ) ); } else { $dimensions = array( $newWidth, $newHeight ); } $newImg = imagecreatetruecolor( $dimensions[0], $dimensions[1] );imageCopyResampled($newImg, $this->img, 0, 0, 0, 0, $dimensions[0], $dimensions[1], $width, $height); if( empty( $copyScaled ) ) { imageDestroy( $this->img ); $this->img = imagecreatetruecolor( $dimensions[0], $dimensions[1] ); imageCopy( $this->img, $newImg, 0, 0, 0, 0, $dimensions[0], $dimensions[1] ); } else { $$copy = new Image( imageJpeg( $newImg ) ); } imageDestroy( $newImg ); return( 1 ); } function ratio( $max ) { $width = imageSX( $this->img ); $height = imageSY( $this->img ); if( empty( $max ) ) { return( 0 ); } $ratio = round( ( $max / max( $width, $height ) ), 2 ); // work out interpolation (scale larger than original) if( ! IMG_IP ) { if( $ratio < 1 ) { $width = round( $width * $ratio ); $height = round( $height * $ratio ); } } else { $width = round( $width * $ratio ); $height = round( $height * $ratio ); } return( Array( $width, $height, $ratio ) ); } function overlay( $img, $ovr, $copy = '' ) { } function parseImgTag( $tag ) { /*format '$url;$args' args: '$attr,$param[[,$param],$paramX];$attr,$param[[,$param],$paramX]' */ $url = substr( $tag, 0, (strpos( $tag, ';' )?strpos($tag,';'):strlen($tag)) ); //print $url; // load image or die if( !$this->ImageFromFile( $url ) ) { header( 'Content-Type: image/jpeg' );$imgPath = 'blank.jpg';function imageCreateFromJpegEx($file){ $data = file_get_contents($file); $im = @imagecreatefromstring($data); $i = 0; while (!$im) { $data = substr_replace($data, "", -3, -2); $im = @imagecreatefromstring($data); } return $im;}$im = imageCreateFromJpegEx($imgPath);imagejpeg( $im ); } $args = substr( strchr( $tag, ';' ), 1 ); // parse args $commands = explode( ';', $args ); foreach( $commands as $command ) { $attr = substr( $command, 0, strpos( $command, ',' ) ); $params = explode( ',', substr( strchr( $command, ',' ), 1 ) ); switch( $attr ) { /* s.. Scale functions as follows s,width,height -- scale to desired dimensions sm,max -- scale to maximum sr,ratio -- scale to ratio (relative to 1.0) */ case 's': $this->scale_image( $params[0], $params[1] ); break; case 'sm': $this->scale_image( $params[0], $params[1] ); break; case 'sr': $this->scale_image( 0, 0, $params[0] ); break; } } $this->p(); }}/* main logic if called directly */if($_SERVER['PATH_TRANSLATED'] == __FILE__) { if(isset($_GET['img']) && !empty($_GET['img'])) { $imgO = new Image(); $imgO->parseImgTag(urldecode($_GET['img'])); }}?>