Page 1 of 1
Max Image size
Posted: Wed Jun 08, 2005 9:25 am
by asterinex
Hello Everyone ,
I have the following problem.
I have a link website with links to other sites.
Because I don't have enough Space on my website I store the pictures url in my database and display them with html <img> tag.
The problem is that many pictures are too big, but I don't want to resize the pictures that are ok.
Is it possible to set a max x,y -size in the image tag. So that if the image exceeds the x max. I correct istself.
Hope I made myself clear enough.
Thanks
Gert
Posted: Wed Jun 08, 2005 9:43 am
by wwwapu
Here is a function I have used in some galleryproject. The parameters are filename and directory where images are.
This creates a thumbnail picture and returns its name. Can be called like
Code: Select all
<img src="e;'.luokuva($images_name_and_path, $kansio).'"e; alt="e;'.$alternative_text.'"e;>
Code: Select all
function luokuva($tiedosto, $kansio){
$kuvan_nimi=dirname($tiedosto)."/mini_".basename($tiedosto);
if(is_file($kuvan_nimi)){
return $kuvan_nimi;
}else{
$data=getimagesize($tiedosto); //array{width , height, type, size_string}types:1 = GIF, 2 = JPG, 3 = PNG
$mX=200; //width of the thumbnail
$mY=$data[1]*$mX/$data[0];
$im=imagecreatetruecolor($mX, $mY);
switch($data[2]){
case '2':
$im2=imagecreatefromjpeg($tiedosto);
imagecopyresized($im, $im2, 0,0,0,0,$mX, $mY, $data[0], $data[1]);
imagejpeg($im, $kuvan_nimi);
break;
case '3':
$im2=imagecreatefrompng($tiedosto);
imagecopyresized($im, $im2, 0,0,0,0,$mX, $mY, $data[0], $data[1]);
imagepng($im, $kuvan_nimi);
break;
case '1':
$im2=imagecreatefromgif($tiedosto);
imagecopyresized($im, $im2, 0,0,0,0,$mX, $mY, $data[0], $data[1]);
imagegif($im, $kuvan_nimi);
break;
}
imagedestroy($im);
return $kuvan_nimi;
}
}
Posted: Wed Jun 08, 2005 10:13 am
by scorphus
Alternatively, you can use CSS to set max-width and max-height.
Code: Select all
<img alt="e;їalternative-text]"e; src="e;image_name.png"e; style="e;max-height: 250px; max-width: 250px;"e; width="e;500"e; height="e;199"e; />
<!-- additional line for mozilla users -->
Although it doesn't work in Internet Exploder, I hope it helps.
-- Scorphus
Posted: Wed Jun 08, 2005 10:32 am
by asterinex
Thanks alot for your responses!
This is wat I did. Maybe it can help someone.
If the size of the picure exceeds 200(either x or y) it %scales the pic.
Code: Select all
$data=getimagesize($myrow['url_of_grafic']);
$pic_w = $data[0];
$pic_h = $data[1];
$max_pix = 200;
if ($pic_w>$pic_h)
$longest=$pic_w;
else
$longest=$pic_h;
$new_widht = $pic_w;
$new_height = $pic_h;
if ($longest>$max_pix)
{
$divnum= $longest/$max_pix;
$new_widht = $pic_w/$divnum;
$new_height = $pic_h/$divnum;
}