note 52421 added to function.getimagesize

From: Date: Sat, 30 Apr 2005 11:24:09 +0000
Subject: note 52421 added to function.getimagesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-88789@lists.php.net to get a copy of this message
I've wrote this piece of useful code. May be it will be useful for you. But i got a problem - if source image is in the area with need of authorization then the functions that read some files from that place (i.e. getimagesize, imagejpeg) does not work! How to solve it? <?php //i't a stand-alone file named resize.php //the feature is caching // /image/thumbcache folder is used with file name forming by md5($img.$calc_width.$calc_height); //it gets such parameters: //img - image address (URL) //w - optional width //h - optional height //if you set either w or h, then the image is resized proportionaly, according to the source //if you set neither w nor h then the script just output file //if you set both w or h then the image will be resized exactly how you want $server_root = 'http://'.$_SERVER['SERVER_NAME'].'/'; if (isset($_GET['img']) && ((isset($_GET['w']) || isset($_GET['h']))) { $img = substr($_GET['img'],0,100); if (isset($_GET['w'])) $w = substr($_GET['w'],0,10); if (isset($_GET['h'])) $h = substr($_GET['h'],0,10); error_reporting(0); //check cache $hash = md5($img.$w.$h); $pos = strrpos($img,"."); $ext = substr($img,$pos+1,strlen($img)-$pos); $fname = $hash.'.'.$ext; $cachedim = @imagecreatefromjpeg($serverroot.'images/thumbcache/'.$fname); if ($cachedim) //just show cached thumbnail { header("Content-type: image/jpeg"); imagejpeg($cachedim,'',100); } else //create and cache thumbnail and show it 'cause it's not in cache { list($width, $height, $type, $attr) = getimagesize($img); if ($type==2) //jpeg { $im = @imagecreatefromjpeg($img); /* Attempt to open */ if (!$im) { /* See if it failed */ $im = imagecreate(150, 30); /* Create a blank image */ $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 1, 5, 5, "Error loading image!", $tc); } else { //constrain proportions if needed if (isset($w)){ if ($w==0) $w = $width; } else $w = $width*$h/$height; if (isset($h)){ if ($h==0) $h = $height; } else $h = $height*$w/$width; $dstw=isset($w)?$w:$width; $dsth=isset($h)?$h:$height; $tim = imagecreatetruecolor($dstw,$dsth); imagecopyresampled($tim,$im,0,0,0,0,$dstw,$dsth,$width,$height); header("Content-type: image/jpeg"); imagejpeg($tim,'./images/thumbcache/'.$fname,100); imagejpeg($tim,'',100); } } } error_reporting(E_ALL); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.getimagesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52421 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52421&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52421&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52421&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52421&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52421&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52421&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88789) next »