Jump to content

Recommended Posts

I got this little piece of code from a guy online. It's in my image upload script to create thumbnails when a user uploads an image. As it is now it creates a 150 x 150 square(distorted). I would like it to possibly resize only width, or only height depending on the shape of the image. For instance, if the image is wider than high, then it gets it's width reduced to 150 and the height would be relatively reduced(as to not distort). Then vice-versa for the height.(height 150 x relative width). Instead of display it goes straight into file system and Mysql name value.

Can anyone spot what I need to change to get those results(or offer alternative)? Thank you:
[code]function create_resize_save_jpeg( $path, $newpath ) {

list( $width_orig, $height_orig ) = getimagesize( $path );
$image_p = imagecreatetruecolor( 150, 150);
if( !$image = imagecreatefromjpeg( $path ) ) {
$_SESSION['errormsg'] = "Cant create photo!";
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 150, 150, $width_orig, $height_orig);
imagejpeg($image_p, $newpath, 100);
}
}

function create_resize_save_gif( $path, $newpath ) {

list( $width_orig, $height_orig ) = getimagesize( $path );
$image_p = imagecreatetruecolor( 150, 150);
if( !$image = imagecreatefromgif( $path) ) {
$_SESSION['errormsg'] = "Cant create proto!";
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 150, 150, $width_orig, $height_orig);
imagegif($image_p, $newpath, 100);
}
}

function create_resize_save_png( $path, $newpath ) {

list( $width_orig, $height_orig ) = getimagesize( $path );
$image_p = imagecreatetruecolor( 150, 150);
if( !$image = imagecreatefrompng( $path ) ) {
$_SESSION['errormsg'] = "Cant create proto!";
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 150, 150, $width_orig, $height_orig);
imagepng($image_p, $newpath, 100);
}
}

function file_extention( $str ) {
$size = strlen($str);
$x=0;

while($x < $size)
{
if($str[$x] == '.')
{
$ext = "";
$ext_len = ($size - $x);
$y=$x;
while($y < $size)
{
$ext .= $str[$y];
$y++;
}
}
$x++;
}
return $ext;
}[/code]
Guest
This topic is now 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.