Jump to content

SimpleImage.php help


conan318

Recommended Posts

I am trying use simple image to resize images as i upload but i cant get it to work.. any ideas where i am going wrong

 

 

simpleimage.php

<?php

/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

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

}
?>

 

 

form.php

 

<?php
   if( isset($_POST['submit']) ) {

      include('../php/SimpleImage.php');
      $image = new SimpleImage();
      $image->load($_FILES['uploaded_image']['tmp_name']);
      $image->resizeToWidth(150);
      $image->output();
   } else {

?>

   <form action="../php/gallery.php" method="post" enctype="multipart/form-data">
      <input type="file" name="uploaded_image" id="uploaded_image" />

      <input type="submit" name="submit" value="Upload" />

   </form>

<?php

}
?>

 

 

gallery.php

 


if ($_FILES['uploaded_image']['size']!= 0){


$ext_a = explode(".", $_FILES['uploaded_image']['name']);

$ext = $ext_a[1];
if ( ($ext!=="gif" && $ext!=="JPG" && $ext!=="png" && $ext!=="jpg" )){
echo "Error IMAGES MUST BE EITHER PNG, GIF OR JPG ";
echo "<a href='gal.php'>Go back</a>";
die;
}

/* hashes the file name with the date and time to genrate a uniue file name*/
$img=md5($_FILES['uploaded_imagee']['name'] . date("m.d.y H:m:s")) . "." . $ext;
$target_path = "../gallery/";

$target_path = $target_path .basename($img); 

$insert_user=mysql_query("INSERT INTO mongrels_db.gallery (img) VALUES ('$img')") ;
if(move_uploaded_file($_FILES['uploaded_image']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploaded_image']['name']). 
    " has been uploaded";
echo"<br>";


}
}else{
echo"<p style='font-size: 20px; color:red;' >";
echo"There was an error in uploading this image";
echo"<br>";
echo"this error may be because the file you are uploading exceeds 2mb";
echo"<br>";
echo"please resize this image";
echo"<br>";
echo"or you did not select a file to upload";
echo"<br>";
echo"<a href='gal.php'>go back</a>";
echo"</p>";


}
?>

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.