WEB Easiest way to resize images on the fly v. ASP/C#

Discussion in 'OT Technology' started by VD, Feb 22, 2008.

  1. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Hey folks, I've got the company directory (that I've posted about before) up and running, and I'm hoping to make it completely self sufficient.

    Right now, I'm having to resize images and put them into two directories, /thumb/ and /resized/

    What I'm hoping to do, is just be able to have them resized by asp via querystring.

    <img src="blah.asp?image=username.jpg">

    All the file names are also the person's username, so this should be pretty easy. I'm not exactly sure how to build/implement it. Someone wanna shed some light?

    I've read a little about the GetThumbnailImage method :dunno:
     
  2. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    I could to it in php in a few lines :dunno:
     
  3. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Hm.

    Well, PHP is installed on that server - perhaps I'll just do it in PHP. :dunno:
     
  4. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    I've done it in PHP and using javascript on the client side. With the javascript one the client gets the big picture then it auto resizes in the browser and is then clickable to get the big image. I integrated that with the lightbox viewer ... kinda neat. I have them both running on my forums if you want to see an example.
     
  5. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Something like that will be great once I get around to allowing users to upload their own images - which I'm hesitant for :o
     
  6. HighLife

    HighLife New Member

    Joined:
    Dec 16, 2007
    Messages:
    9,445
    Likes Received:
    0
    Location:
    Kansas
    I'll write the PHP for you in a second, I already wrote the class that controls this + watermarking
     
  7. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    Here is the image class I made a while ago:

    Code:
    <?php
    
    /*	CLASS NAME: 	IMG
    	AUTHOR:			(edit for OT: just use it, I don't care..)
    	DESCRIPTION: 	Create, resize, watermark, and store images
       						-Handles jpeg, png, and gif images.
    	ASSUMPTIONS:	PHP must have the GD library  installed (available at http://www.libgd.org/).
    					The upload_dir must be writable.
    					In order for show() & store_show() to work, there can be no previous headers/echos
    						or the image output will be corrupt in the browser.
    					To clean up resources correctly, you must use store_show() rather than
    						store() and then show(), if you wish to both store and show an image. */
    
    class IMG {
    	var $image = '';
    	var $name = '';
    	var $temp = '';
    	var $image_quality = '';
    	var $get_dir = '';
    	var $upload_dir = '';
    	
    	function IMG($sourceFile, $imgQuality = 80, $g_dir = '', $u_dir = ''){
    		$this->setdirs($g_dir, $u_dir);
    		$this->image_quality = (int)$imgQuality;
    		if(file_exists($this->get_dir.$sourceFile)){
    			$filetype = strtolower(substr($sourceFile,strlen($sourceFile)-4,4));
    			if($filetype == ".gif")  $this->image = @imagecreatefromgif($this->get_dir.$sourceFile);  
    			if($filetype == ".jpg")  $this->image = @imagecreatefromjpeg($this->get_dir.$sourceFile);  
    			if($filetype == ".png")  $this->image = @imagecreatefrompng($this->get_dir.$sourceFile);
    			if (!$this->image)	$this->errorHandler("Incorrect image file type.", 1);
    			$imagenamearray = explode("/", $sourceFile);
    			$imagename = $imagenamearray[(sizeof($imagenamearray) - 1)];
    			$imagenamearray = explode(".", $imagename);
    			$imagename = $imagenamearray[0];
    			$this->name = $imagename;
    		} else {
    			$this->errorHandler("Image not found.", 2);
    		}
    		return;
    	}
    	
    	function resize($maxDemensions = array(640, 480), $aspectradio = true){
    		list($width, $height) = $maxDemensions;
    		$o_wd = imagesx($this->image);
    		$o_ht = imagesy($this->image);
    		if(isset($aspectradio)&&$aspectradio) {
    			if ($o_wd > $width || $o_ht > $height) {
    				$w = round($o_wd * $height / $o_ht);
    				$h = round($o_ht * $width / $o_wd);
    			
    				if(($height-$h)<($width-$w)){
    					$width =& $w;
    				} else {
    					$height =& $h;
    				}
    			} else {
    				$width = $o_wd;
    				$height = $o_ht;
    			}
    		}
    		$this->temp = imagecreatetruecolor($width,$height);
    		imagecopyresampled($this->temp, $this->image,
    		0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
    		$this->sync();
    		return;
    	}
    	
    	function sync(){
    		$this->image =& $this->temp;
    		unset($this->temp);
    		$this->temp = '';
    		return;
    	}
    	
    	function show(){
    		$this->_sendHeader();
    		if (!$this->image)	$this->errorHandler("An error occured while attempting to display this image.", 3);
    		imagejpeg($this->image, NULL, $this->image_quality);
    		if (isset($this->temp)&&$this->temp !='') imagedestroy($this->temp);
    		imagedestroy($this->image);
    		unset($this->name);
    		return;
    	}
    	
    	function _sendHeader(){
    		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    		header('Content-type: image/jpeg');
    		header('Content-Disposition: inline; filename='.$this->name);
    	}
    	
    	function store($file){
    		imagejpeg($this->image, $this->upload_dir.$file, $this->image_quality);
    		if (isset($this->temp)&&$this->temp !='') imagedestroy($this->temp);
    		imagedestroy($this->image);
    		unset($this->name);
    		return;
    	}
    	
    	function store_show($file){
    		imagejpeg($this->image, $this->upload_dir.$file, $this->image_quality);
    		$this->show();
    		return;
    	}
    	
    	function watermark($watermarkImage, $left = 0, $top = 0){
    		if (!$this->image)	$this->errorHandler("This image could not be watermarked successfully.", 4);
    		$filetype = strtolower(substr($watermarkImage,strlen($watermarkImage)-4,4));
    		if($filetype == ".gif")  $layer = @imagecreatefromgif($this->get_dir.$watermarkImage);  
    		if($filetype == ".jpg")  $layer = @imagecreatefromjpeg($this->get_dir.$watermarkImage);  
    		if($filetype == ".png")  $layer = @imagecreatefrompng($this->get_dir.$watermarkImage);
    		if (!$layer) $this->errorHandler("Incorrect watermark image file type.", 5);
    		$logoW = imagesx($layer); 
    		$logoH = imagesy($layer); 
    		imagecopymerge($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH, 100); 
    	}
    	
    	function setdirs($g_dir, $u_dir) {
    		if ($g_dir != '') {
    			if (substr($g_dir, 0,1) == '/') $g_dir = substr($g_dir,1,strlen($g_dir)-1);
    			if (substr($g_dir, strlen($g_dir)-1,strlen($g_dir)) != '/') $g_dir .= '/';
    		}
    		if ($u_dir != '') {
    			if (substr($u_dir, 0,1) == '/') $u_dir = substr($u_dir,1,strlen($u_dir)-1);
    			if (substr($u_dir, strlen($u_dir)-1,strlen($u_dir)) != '/') $u_dir .= '/';
    		}	
    		$this->get_dir		= $g_dir;
    		$this->upload_dir	= $u_dir;
    	}
    	
    	function find_unique_name($file) {
    		if (file_exists($this->upload_dir.$file)) {
    			$c=1;
    			while (file_exists($this->upload_dir.substr($file, 0, strlen($file)-4).$c.substr($file, strlen($file)-4, 4))) { 
    				$c++; 
    			}
    			return substr($file, 0, strlen($file)-4).$c.substr($file, strlen($file)-4, 4);
    		}	
    		return $file;
    	}	
    	
    	function errorHandler($errString = "Unknown Error", $errNo = 0){
    		// use whatever error handling you have here
    		/* --error number meanings--
    			0	=>	Unknown Error.
    			1	=>	Incorrect Image File Type.
    			2	=> 	Image Not Found
    			3	=>	Error Occured Attempting To Display Image.
    			4	=>	Image Could Not Be Watermarked Successfully.
    			5	=>	Incorrect Watermark Image File Type.
    		   ----------------- */	
    		trigger_error("<p>An error occured while this image was being processed.</p><p>" .
    						"Error Number: $errNo<br><i>$errString</i>", E_USER_ERROR);
    		exit();
    	}
    	
    	
    } // end of IMG class
    
    ?>
    
    Example usage:
    Code:
    <?php
    require_once("IMG.php");
    $img_name = $_GET['image']; // image name from URL
    $pic = new IMG($img_name, 95);  		//	JPEG Quality 95, Image is in same directory as script
    $pic->resize(array(300, 300), true);	//	Resize to max 300x300, keep aspect ratio
    $pic->show();							//	Show image
    ?>
    RAR file with IMG class and a few other usages:
    http://www.filelime.com/upload/files/PHP_IMG.rar


    Good luck :wavey:
     
  8. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    I guess it has to be done in asp.

    Someone help :x:
     

Share This Page