Jump to content

Image resizing without distorting the image


skru09

Recommended Posts

Hi,

 

I have php page wherein I am retrieving images from the database and displaying them. The images are of different sizes hence i want to resize the images without them being distorted. I used the following code:

 

<style type = "text/css">

.faculty-image {

max-width: 99px;

border: 1px solid #999;

}

* html .faculty-image { width: 99px; height: 100px}

</style>

 

<?php

//connected to the db and retrieved the images

 

$rep='<class = "/faculty-image/" />';

$image=str_replace("/>",$rep,$img);

 

//have done the above as the image field retrieved is of the form <img src="/abc.jpg" title="abc" alt="abc" height="235" width="235" />

 

echo $image;

?>

 

But the images are not displayed in an uniform manner.

 

Please guide me on this.Thanks

 

Link to comment
Share on other sites

Hi

 

Thanks for the link. I did go through the link but i think the function will resize the image proportionately. But i need all the images to be resized to one particular size.

 

Please correct my understanding if I am wrong.

 

Thanks.

Link to comment
Share on other sites

I made a simple library to resize images a while ago.

 

You specify the input file max size, say 250px, and the lib does all the work for you. It'll resize the image in proportion by working out which side is bigger, changing it to your specified size and changing the other side in proportion.

 

http://pastebin.com/4DWreHXC

 

Includes example code and documentation.

Link to comment
Share on other sites

This is what I use:

 

function resize_image($sourcefile, $destfile, $fw, $fh,$jpegquality = 100){
    if(!file_exists($sourcefile))die("Cannot Read sourcefile: ".$sourcefile);
//determine type, height, width
list($ow, $oh, $from_type) = getimagesize($sourcefile)  or die('3');
switch($from_type){
case 1: // GIF
	$srcImage = imageCreateFromGif($sourcefile);
	break;
case 2: // JPG
	$srcImage = imageCreateFromJpeg($sourcefile) or die('1');
	break;
case 3: // PNG
	$srcImage = imageCreateFromPng($sourcefile);
	break;
default: // any other format
	return FALSE;
	break;
}
//determine dimensions for full size
if($fw>$ow)$fw=$ow;
$tempw = $fw;
$temph = number_format((($oh*$fw)/$ow), 0);
if($temph > $fh && $fh > 0){
   $tempw = number_format((($ow*$fh)/$oh), 0);
   $temph = $fh;
}
//create the resized image
$tempImage = imageCreateTrueColor($tempw, $temph);
imagecopyresampled($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh);
//grab the resized images blob
imageJpeg($tempImage, $destfile, $jpegquality);
$imageblob=file_get_contents($destfile);
//destroy file
unlink($destfile);
    return $imageblob;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.