irwan

Simple Image Resize Class

Nov 17th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. FILE 1 : index.php
  2.  
  3. <html>
  4. <body>
  5. <form action="upload.php" enctype="multipart/form-data" method="post">
  6. <input name="file" size="35" type="file"/>
  7. <input name="submit" type="submit" value="Upload"/>
  8. </form>
  9.  
  10. </body>
  11. </html>
  12.  
  13.  
  14.  
  15.  
  16.  
  17. FILE 2 : upload.php
  18.  
  19. <?php
  20.  
  21. require_once "upload.class.php";
  22. if(isset($_FILES['file'])) {
  23. $uploader = new uploader($_FILES['file']);
  24. $uploader->upload();
  25.  
  26. $ok = $uploader->getInfo();
  27.  
  28. if(!empty($ok))
  29. {
  30. $photo = $uploader->dir . $uploader->file['name'];
  31. echo $uploader->getInfo();
  32. echo "<br/>";
  33. }
  34. else
  35. {
  36. echo $uploader->getError();
  37. echo "<br/>";
  38. }
  39. }
  40. ?>
  41.  
  42.  
  43.  
  44.  
  45.  
  46. FILE 3 : upload.class.php
  47.  
  48. <?php
  49. class uploader{
  50.  
  51.  private $type = array("jpg","jpeg","gif","png"),$width = 240,$height = 240,$info = '',$error='';
  52.  
  53.  function __construct($file,$dir="./"){
  54.  $this->file = $file;
  55.  $this->dir = $dir;
  56.  }
  57.  
  58.  /**
  59.  * Resize and upload an image.
  60.  * @return String fileName or False on error
  61.  */
  62.  public function upload(){
  63.  $ext = explode(".",$this->file['name']);
  64.  $ext = strtolower(end($ext));
  65.  
  66.  if(file_exists($this->dir.$this->file['name'])){
  67.  $this->error .= '<b>Error: file already exists.</b>';
  68.  return false;
  69.  }
  70.  if (!in_array($ext,$this->type)){
  71.  $this->error .= '<b>Error: file format not supported.</b>';
  72.  return false;
  73.  }
  74.  
  75.  // Get image size
  76.  list($imwidth,$imheight) = getimagesize($this->file['tmp_name']);
  77.  
  78.  $hx = (100 / ($imwidth / $this->width)) * .01;
  79.  $hx = round ($imheight * $hx);
  80.  
  81.  if ($hx < $this->height) {
  82.  $this->height = (100 / ($imwidth / $this->width)) * .01;
  83.  $this->height = round ($imheight * $this->height);
  84.  } else {
  85.  $this->width = (100 / ($imheight / $this->height)) * .01;
  86.  $this->width = round ($imwidth * $this->width);
  87.  }
  88.  
  89.  // Create a new true color image
  90.  $image = imagecreatetruecolor($this->width, $this->height);
  91.  
  92.  // Create a new image from file
  93.  if($ext == "jpg" || $ext == "jpeg") {
  94.  $im = imagecreatefromjpeg ($this->file['tmp_name']);
  95.  } else if($ext == "gif") {
  96.  $im = imagecreatefromgif ($this->file['tmp_name']);
  97.  } else if($ext == "png") {
  98.  $im = imagecreatefrompng ($this->file['tmp_name']);
  99.  }
  100.  
  101.  // Copy and resize part of an image with resampling
  102.  if(imagecopyresampled($image, $im, 0, 0, 0, 0, $this->width, $this->height, $imwidth, $imheight)){
  103.  $this->info .= '<b>Image Uploaded Successfully!</b>';
  104.  }
  105.  
  106.  // Output image
  107.  if($ext == "jpg" || $ext == "jpeg") {
  108.  imagejpeg($image, $this->dir.$this->file['name'], 100);
  109.  } else if($ext == "gif") {
  110.  imagegif ($image, $this->dir.$this->file['name']);
  111.  } else if($ext == "png") {
  112.  imagepng ($image, $this->dir.$this->file['name'], 0);
  113.  }
  114.  
  115.  // Destroy an image
  116.  imagedestroy($im);
  117.  return $im;
  118.  
  119.  }
  120.  
  121.  /**
  122.  * Get uploading information.
  123.  */
  124.  public function getInfo(){
  125.  return $this->info;
  126.  }
  127.  
  128.  /**
  129.  * Get uploading error.
  130.  */
  131.  public function getError(){
  132.  return $this->error;
  133.  }
  134.  
  135. }
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment