Max Image size

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
asterinex
Forum Commoner
Posts: 52
Joined: Thu Nov 25, 2004 7:01 am
Location: Belgium

Max Image size

Post 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
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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

&lt;img src=&quote;'.luokuva($images_name_and_path, $kansio).'&quote; alt=&quote;'.$alternative_text.'&quote;&gt;

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;
	}
}
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Alternatively, you can use CSS to set max-width and max-height.

Code: Select all

&lt;img alt=&quote;&#1111;alternative-text]&quote; src=&quote;image_name.png&quote; style=&quote;max-height: 250px; max-width: 250px;&quote; width=&quote;500&quote; height=&quote;199&quote; /&gt;
&lt;!-- additional line for mozilla users --&gt;
Although it doesn't work in Internet Exploder, I hope it helps.

-- Scorphus
asterinex
Forum Commoner
Posts: 52
Joined: Thu Nov 25, 2004 7:01 am
Location: Belgium

Post by asterinex »

Thanks alot for your responses! :D

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;
}
Post Reply