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

 

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.

Might also look at...

 

Simple Image Manipulation in PHP (Rotate, Resize, Crop, Flip and Mirror, Thumbnails square and regular)

 

http://www.mawhorter.net/web-development/simple-image-manipulation-in-php-rotate-resize-crop-flip-and-mirror-thumbnails-square-and-regular

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.