how do i define img maximum size?

  • pedrotuga
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

i have this php script that lays out articles containing external images.

how can i be shure that the image does not breaks out of the div if its bigger than it?

basically i need to define an image maximum size so if the image is bigger than that it will shrink so it fits.

how can i do this. is there a way of doing this with css? if not i can use html, the img tags are generated by the script with i can edit.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Have a look at this article
  • pedrotuga
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

thank you very much. That was exactly what i was looking for.

just a question: what did you googled for? cause i spent one and half hour googling without success :(
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

:D

I searched for image max width


Second result on google.co.uk

(5 seconds)
  • oi_antz
  • Born
  • Born
  • oi_antz
  • Posts: 4

Post 3+ Months Ago

Code: [ Select ]
<?php
    public static function resize($fileName, $w, $h, $jpgQuality=80, $prop=true, $pad=true, $bgColor='')
    {
        // bg color is an array as rgb code
        if($bgColor == '') $bgColor = array(255, 255, 255);
        
        $dims=getimagesize($fileName);
        
        if($prop){
            // calculate proportions
            if(($dims['0']/$w)>($dims['1']/$h)){
                // width rules
                $newW = $w;
                $newH = (($new*$subject)/$orig);
            }else{
                //height rules
                $newH = $h;
                $newW = (($new*$subject)/$orig);     
            };
            if($pad){
                if($newW < $w){
                    // have to pad vertically
                    $innerW = $newW;
                    $newW = $w;
                    $lPos = (($w-$innerW)/2);
                }else{
                    $lPos = 0;
                    $innerW = $w;
                }
                if($newH < $h){
                    // have to pad horizontally
                    $innerH = $newH;
                    $newH = $h;
                    $tPos = (($h-$innerH)/2);
                }else{
                    $tPos = 0;
                    $innerH = $h;
                }
                //echo 'innerH: '.$innerH.', innerW: '.$innerW.', newH: '.$newH.', newW: '.$newW.', tpos: '.$tPos.', lPos: '.$lPos.'<br />';
            };
        }else{
            $newW = $w;
            $newH = $h;
        }
        $ext = substr($fileName, strlen($fileName)-4);
        //echo $fileName.'<br />';
        switch($ext){
            case'.jpg':
             global $jpg_quality;
             $image = imagecreatefromjpeg($fileName);
             $image_p = imagecreatetruecolor($newW, $newH);
             $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
             imagefill($image_p, 0, 0, $color);
             if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
             else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
//echo 'image: '.$image_p.', '.$fileName.' , '.$jpgQuality.' , '.$w. ' , '.$h.'<br />';
             $image = imagejpeg($image_p, $fileName, $jpgQuality);
            break;
            case'.gif':
             $image = imagecreatefromgif($fileName);
             $image_p = imagecreatetruecolor($newW, $newH);
             $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
             imagefill($image_p, 0, 0, $color);
             if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
             else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
             $image = imagegif($image_p,$fileName);
            break;
            case'.png':
             $image = imagecreatefrompng($dest);
             $image_p = imagecreatetruecolor($newW, $newH);
             $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
             imagefill($image_p, 0, 0, $color);
             if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
             else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
             $image = imagepng($image_p,$fileName);
            break;
        };
    }
    
  1. <?php
  2.     public static function resize($fileName, $w, $h, $jpgQuality=80, $prop=true, $pad=true, $bgColor='')
  3.     {
  4.         // bg color is an array as rgb code
  5.         if($bgColor == '') $bgColor = array(255, 255, 255);
  6.         
  7.         $dims=getimagesize($fileName);
  8.         
  9.         if($prop){
  10.             // calculate proportions
  11.             if(($dims['0']/$w)>($dims['1']/$h)){
  12.                 // width rules
  13.                 $newW = $w;
  14.                 $newH = (($new*$subject)/$orig);
  15.             }else{
  16.                 //height rules
  17.                 $newH = $h;
  18.                 $newW = (($new*$subject)/$orig);     
  19.             };
  20.             if($pad){
  21.                 if($newW < $w){
  22.                     // have to pad vertically
  23.                     $innerW = $newW;
  24.                     $newW = $w;
  25.                     $lPos = (($w-$innerW)/2);
  26.                 }else{
  27.                     $lPos = 0;
  28.                     $innerW = $w;
  29.                 }
  30.                 if($newH < $h){
  31.                     // have to pad horizontally
  32.                     $innerH = $newH;
  33.                     $newH = $h;
  34.                     $tPos = (($h-$innerH)/2);
  35.                 }else{
  36.                     $tPos = 0;
  37.                     $innerH = $h;
  38.                 }
  39.                 //echo 'innerH: '.$innerH.', innerW: '.$innerW.', newH: '.$newH.', newW: '.$newW.', tpos: '.$tPos.', lPos: '.$lPos.'<br />';
  40.             };
  41.         }else{
  42.             $newW = $w;
  43.             $newH = $h;
  44.         }
  45.         $ext = substr($fileName, strlen($fileName)-4);
  46.         //echo $fileName.'<br />';
  47.         switch($ext){
  48.             case'.jpg':
  49.              global $jpg_quality;
  50.              $image = imagecreatefromjpeg($fileName);
  51.              $image_p = imagecreatetruecolor($newW, $newH);
  52.              $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
  53.              imagefill($image_p, 0, 0, $color);
  54.              if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
  55.              else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
  56. //echo 'image: '.$image_p.', '.$fileName.' , '.$jpgQuality.' , '.$w. ' , '.$h.'<br />';
  57.              $image = imagejpeg($image_p, $fileName, $jpgQuality);
  58.             break;
  59.             case'.gif':
  60.              $image = imagecreatefromgif($fileName);
  61.              $image_p = imagecreatetruecolor($newW, $newH);
  62.              $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
  63.              imagefill($image_p, 0, 0, $color);
  64.              if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
  65.              else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
  66.              $image = imagegif($image_p,$fileName);
  67.             break;
  68.             case'.png':
  69.              $image = imagecreatefrompng($dest);
  70.              $image_p = imagecreatetruecolor($newW, $newH);
  71.              $color = imagecolorallocate($image_p, $bgColor[0], $bgColor[1], $bgColor[2]);
  72.              imagefill($image_p, 0, 0, $color);
  73.              if(!$pad) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newW, $newH, $dims['0'], $dims['1']);
  74.              else imagecopyresampled($image_p, $image, $lPos, $tPos, 0, 0, $innerW, $innerH, $dims['0'], $dims['1']);
  75.              $image = imagepng($image_p,$fileName);
  76.             break;
  77.         };
  78.     }
  79.     


And you would use like:
Code: [ Select ]
$filename = public_html/images/img1.gif;
resize($filename, $newWidth, $newHeight);
  1. $filename = public_html/images/img1.gif;
  2. resize($filename, $newWidth, $newHeight);

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 50 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.