HTML & PHP: Images help

Discussion in 'Programming' started by MCJim, Jun 25, 2008.

  1. #1
    1. I'm trying to scale images so they remain in proportion with the original image, but do not exceed certain sizes. How can I do this? I can't just take a percentage of the image or strictly set it to a certain size, because the original image sizes vary greatly.

      The best example of this is here: http://zip.4chan.org/v/imgboard.html

    2. How do I restrict the image so it only appears in a certain area of the page without affecting anything else? I'm dynamically loading images, and I don't the differing sizes to alter text on the page.

    Thanks in advance.
     
    MCJim, Jun 25, 2008 IP
  2. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK, I found out how to do number 1 by using max-height and max-width. I'm still not sure how to do number 2, however.
     
    MCJim, Jun 25, 2008 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    I wrote a GD based function for one of my projects, if you use GD with PHP following can help you. Here you go:

    Functio to include:

    	function gd_fit(&$image, $target) { 
    		$width = imagesx($image);
    		$height = imagesy($image);
    		
    		if ($width > $height) 
    			$percentage = ($target / $width); 
    		else
    			$percentage = ($target / $height); 
    		$width = round($width * $percentage); 
    		$height = round($height * $percentage); 
    		$ret=@imagecreatetruecolor($width, $height); 
    		@imagecopyresampled ($ret,$image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));  		
    		return $ret;
    	} 
    PHP:

    Usage:

    $img = imagecreatefromjpeg("test.jpg"); //suppose it is 800x600
    $img = gd_fit($img, 100); //now is fit to 100x100 boundry with aspect ratio 
    imagejpeg($img,"resized.jpg",100); //save new image 
    PHP:

    I hope it helps.

    regards
     
    Vooler, Jun 26, 2008 IP