Jump to content

need help in resizing image


doforumda

Recommended Posts

hi i am trying to upload an image and resize it to thumbnail size. how can i resize that image which is being uploaded?

 

i want to resize my image to width=50 and height=50

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"  /><p>
    <input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

 

fileupload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp_name = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

function getExtension($str) {
 $i = strrpos($str,".");
 if (!$i) { return ""; }
 $l = strlen($str) - $i;
 $ext = substr($str,$i+1,$l);
 return $ext;
}

$ext = getExtension($name);
$ext = strtolower($ext);

if($error == 0) {
$location = "upload/".$name;
if($ext == "png" && $ext == "jpg" && $ext == "jpeg" && $ext == "gif") {
	if($size > 2000000) {
		if(!file_exists($location)) {
			move_uploaded_file($temp_name,$location);
			//query database to add location and image name.
			echo "Upload Complete.";
		}
		else
			die("File already exist.");
	}
	else 
		die("format is not allowed or file size is too big");
}
else
	die("it is too big size.");
}
else
die("Error uploading file! code $error.");

?>
</body>
</html>

Link to comment
Share on other sites

You coudn't resize the image as its being uploaded but once its on the server you can resize it using imagecopyresampled check the php.net site for the documentation on that or if you wanted to cheat you could download the WideImage PHP class (Google it) that has a very simple and easy to use set of functions its also capable of doing most other things with images.

 

It is based around the GD library though so if your planning a big launch of anything I'd look into using Image Magick :)

 

 

Link to comment
Share on other sites

im resizing my image too, you wanna know how?

heres the class you will need:

 

<?php
class SimpleImage {
   
   var $image;
   var $image_type;

   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}
?>

 

save this class as simpleimage.php

and i did the same thing as you did, this is my upload page:

 

<?php
session_start();
include('SimpleImage.php');
function file_extension($filename)
{
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}

$upfile = $_FILES['uploadedfile']['name'];
$fille = file_extension($upfile);

$gebruikers = $_SESSION['inloggen'];
if($fille == "jpg") { 
$target_path = "uploads/" . $gebruikers . ".jpg";
} elseif($fille == "png") {
$target_path = "uploads/" . $gebruikers . ".png";
} elseif($fille == "gif") {
$target_path = "uploads/" . $gebruikers . ".gif";
} 
if(file_exists($target_path)) {
unlink($target_path);
}
if ($fille == "jpg" || $fille == "png" || $fille == "gif") {
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The image ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded successfully!!";
$image = new SimpleImage();
$image->load($target_path);
$image->resize(50,50);
$image->save($target_path);
} else {
echo "Don't make the image too big to upload!!";
}

} else
{
echo "Wrong file! use jpg, png or gif!!";	
}


?>

 

i hope this works for you. it does work for me

Link to comment
Share on other sites

now i am using imagecopyresized and my code is working fine. the problem only is when my image is resizes to thumbnail then its quality gets bad. Why is that and how can i maintain the quality of my image?

<?php
function imagetothumb($imagename, $extension) {
$save = "thumb/".$imagename;
$filename = $imagename;//'image.jpg';
$percent = 0.05;
//$width = 50;
//$height = 50;

header('Content-type: image/'.$extension);

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb,$save,100);
}


function getExtension($str) {
 $i = strrpos($str,".");
 if (!$i) { return ""; }
 $l = strlen($str) - $i;
 $ext = substr($str,$i+1,$l);
 return $ext;
}
$imagename = "image3.jpg";
$ext = getExtension($imagename);
$ext = strtolower($ext);

//imageresizer($imagename,$ext);

imagetothumb($imagename,$ext);

?>

Link to comment
Share on other sites

You can play with the number at the end of the line I gave you (the max is 9) see what looks best. I use Image Magick so I can't remember if imagepng('output.png',0); is lossless or imagepng('output.png',9); but try adjusting that number see what you can do. Maybe post a link to an image you've uploaded?

 

 

Link to comment
Share on other sites

Firstly you'll have to check your phpinfo(); to see if your web server supports it.

 

Then you will need to decide whether you want to do it from a shell point of view or using a wrapper (MagickWand) to run it.

 

I personally prefere using shell ie running it like this

 

passthru('convert sourcepic.jpg -rotate 45 save_as_pic.png ',$out);

 

And its as easy as that. But you do need shell access so try running it and maybe ask your hosting provider if they will enable it (if disabled)

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.