how do i define img maximum size?
- pedrotuga
- Proficient


- Joined: Apr 19, 2005
- Posts: 315
- Status: Offline
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.
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


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
April 26th, 2006, 12:26 am
- Zwirko
- Guru


- Joined: May 29, 2005
- Posts: 1417
- Loc: 55° N, 3° W
- Status: Offline
- pedrotuga
- Proficient


- Joined: Apr 19, 2005
- Posts: 315
- Status: Offline
- Zwirko
- Guru


- Joined: May 29, 2005
- Posts: 1417
- Loc: 55° N, 3° W
- Status: Offline
- oi_antz
- Born


- Joined: Jul 27, 2008
- Posts: 4
- Status: Offline
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;
};
}
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;
};
}
- <?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;
- };
- }
And you would use like:
Code: [ Select ]
$filename = public_html/images/img1.gif;
resize($filename, $newWidth, $newHeight);
resize($filename, $newWidth, $newHeight);
- $filename = public_html/images/img1.gif;
- resize($filename, $newWidth, $newHeight);
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 5 posts
- Users browsing this forum: No registered users and 57 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
