user1570257
user1570257

Reputation: 21

php picture resize not working on image upload

I'm trying to resize an image that's being uploaded on my php form. The picture is being saved but not resizing. Basically, everything is working fine except for the resizing of the image part. I put my code below.

html form with php for re sizing

<?php include "base.php"; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Submit an Album</title>
</head>
<body>

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

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

?>

     <table>
        <tr>
        <td align="center">Submit an Album</td>
        </tr>
        <tr>
        <td>
     <table>
        <form enctype="multipart/form-data" action="submitAlbumForm.php" method="post">
        <tr>
          <td>Artist Name</td>
          <td><input type="text" name="artistName" size="20">
          </td>
        </tr>
        <tr>
          <td>Album Name</td>
          <td><input type="text" name="albumName" size="20">
          </td>
        </tr>
        <tr>
        <tr>
          <td>Release Date</td>
          <td><input type="text" name="releaseDate" size="20">
          </td>
        </tr>
        <tr>        
        <tr>
          <td>Leak Date</td>
          <td><input type="text" name="leakDate" size="20">
          </td>
        </tr>
        <tr>
        <tr>
          <td>Where It Leaked</td>
          <td><input type="text" name="whereItLeaked" size="20">
          </td>
        </tr>
        <tr>
        <tr>
          <td>Album Cover</td>
          <td><input type="file" name="albumCover">
          </td>
        </tr>
        <tr>        
          <td></td>
          <td align="right"><input type="submit" name="submit" value="Add"></td>
        </tr>
        </form>
        </table>
      </td>
    </tr>
</table>

<?php
   }
?>

</body>
</html>

submitAlbumForm.php, the php form for uploading form info to database

<?php
include "base.php";

//Setting up images directory
$target = "images/"; 
$target = $target . basename( $_FILES['albumCover']['name']);

$albumCover=($_FILES['albumCover']['name']); 

//inserting data order
$order = "INSERT INTO albums
            (artistName, albumName, releaseDate, leakDate, whereItLeaked, albumCover)
            VALUES
            ('$_POST[artistName]',
            '$_POST[albumName]',
            '$_POST[releaseDate]',
            '$_POST[leakDate]',
            '$_POST[whereItLeaked]',
            '{$_FILES['albumCover']['name']}')";

 //Writes image to database
 mysql_query("INSERT INTO `albums` VALUES ('albumCover')") ; 

  if(move_uploaded_file($_FILES['albumCover']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives an error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 

//declare in the order variable
$result = mysql_query($order);  //order executes
if($result){
    echo("<br>Thank you for submitting!");
} else{
    echo("<br>Sorry, something went wrong! Please try again!");
}
?>

SimpleImage.php, the script i'm using for resizing the image-

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

}
?>

I know I have a lot of security holes right now, and will be working on those soon! Thanks guys!

Upvotes: 0

Views: 1837

Answers (3)

Rawnay
Rawnay

Reputation: 241

you can try my own program. I do upload and store the data and images can be reduced in size that we can change.

i use php and xampp. directory :

C:\xampp\htdocs\phpyan\6_Database_2
6_Database_2 :
..images (dir)
..packages (dir)
..input_personal_info.php (file)

..images
   ..small(dir)
   ..more pict (file picture)
-----------------------------
..packages
   ..ClassDatabase.php 
   ..Personal.php

in the "images folder" there is a folder called "small"

notes : and in the package folder I keep the classes to function as a function of storing data and uploading image in small size

input_personal_info.php

<?php
 include 'packages/ClassDatabase.php';
 include 'packages/Personal.php';
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Input Personal Information</title>
  </head>
 <body>
  <h1>Personal Information</h1>
  <form enctype="multipart/form-data" action="" name="frm_personal_info" method="post">
    <table>
     <tr>
      <td>First Name</td>
      <td><input type="text" name="First_Name"></td>
     </tr>
     <tr>
      <td>Last Name</td>
      <td><input type="text" name="Last_Name"></td>
     </tr>
     <tr>
      <td>Date Birth</td>
      <td><input type="text" name="Date_Birth"></td>
     </tr>
     <tr>
      <td>Gender</td>
      <td>
        <select name="Gender">
         <option value="">Gender...</option>
         <option value="Pria">Pria</option>
         <option value="Wanita">Wanita</option>
         <option value="Lainnya">Lain..</option>
        </select>
      </td>
     </tr>
     <tr>
      <td>Photo Profile</td>
      <td><input type="file" name="Photo_Profile"></td>
     </tr>
      <tr>
      <td>About</td>
      <td><textarea name="About" rows="4" cols="30"></textarea></td>
     </tr>
      <tr>
      <td colspan="2"><input type="submit" name="submit" value="Save"></td>
     </tr>
    </table>
  </form>
 </body>
</html>
<?php
// connect with database
$connector = new ClassDatabase("localhost","root","","RawnayDB");
$connector->connect();
 if (isset($_POST['submit'])) {
    // assign values on variabel
      $first_name = $_POST['First_Name'];
    $last_name = $_POST['Last_Name'];
    $date_birth = $_POST['Date_Birth'];
    $gender = $_POST['Gender'];
    $about = $_POST['About'];
    $dir = "images/";
    $dir2 = "images/small/";
    $photo_name = $_FILES['Photo_Profile']['name'];
    $photo_error = $_FILES['Photo_Profile']['error'];
    $photo_type = $_FILES['Photo_Profile']['type'];
    $photo_size = $_FILES['Photo_Profile']['size'];
    $photo_tmp = $_FILES['Photo_Profile']['tmp_name'];

    $personals = new Personal();

    // save file foto
    $personals->uploadPic($dir,$photo_name,$photo_tmp);
    // save pic small
    $personals->thumbPic($dir2,$photo_name);
    // save data
    $personals->savePersData($first_name,$last_name,$date_birth,$gender,$photo_name,$about);


 }
?>

packages/ClassDatabase.php

<?php
// class Database
class ClassDatabase{
  var $host;
  var $user;
  var $pswd;
  var $db;

  function ClassDatabase($host,$user,$pswd,$db){
    $this->host = $host;
    $this->user = $user;
    $this->pswd = $pswd;
    $this->db = $db;
  }

  function connect(){
  // Connection Test
    if(mysql_connect($this->host,$this->user,$this->pswd))
    {mysql_select_db($this->db);} else {echo "Connection Failed";}

  }
}
?>

packages/Personal.php //> class

<?php
// class personal

class Personal{

  var $tabel = "personal";
  var $photo_original;
  var $photo_small;
  var $width;
  var $height;
  var $thumbWidth = 100;
  var $thumbHeight;

  function savePersData($fstnm,$lstnm,$dtbrth,$gndr,$photo_name,$about){
      $small = $this->photo_small;
      $original = $this->photo_original;
      $photo_name = "Pic".$fstnm;
      $query = "INSERT INTO $this->tabel VALUES('','$fstnm','$lstnm','$dtbrth','$gndr','$photo_name','$small','$original','$about');";
      if(mysql_query($query)){
        echo "<script>alert('penyimpanan sukses');</script>";
      }
  }

  function uploadPic($dir,$photo_name,$photo_tmp){
     is_uploaded_file($photo_tmp);
     $check = move_uploaded_file($photo_tmp,$dir.$photo_name);
     if($check){
     $this->photo_original=$dir.$photo_name;
     }
  }

  function thumbPic($dir2,$filename){
    $photo_original = imagecreatefromjpeg($this->photo_original);
    $this->width = imagesx($photo_original);
    $this->height = imagesy($photo_original);
    $this->thumbHeight = ($this->thumbWidth/$this->width)*$this->height;
    // resample picture
    $gbr_thumb = imagecreatetruecolor($this->thumbWidth,$this->thumbHeight);
    imagecopyresampled($gbr_thumb,$photo_original,0,0,0,0,$this->thumbWidth,$this->thumbHeight,$this->width,$this->height);
    // Save your directory
    $acak = rand(00000,999999);
    imagejpeg($gbr_thumb, $dir2."Small_Rawnay_Pic_".$acak.".jpeg");
    $this->photo_small = $dir2."Small_Rawnay_Pic_".$acak.".jpeg";
  }
}
?>

Upvotes: 0

ghbarratt
ghbarratt

Reputation: 11721

I do not see how the if block with if( isset($_POST['submit']) ) ever gets hit and if it does then the image data would be directly output, which would not work since it is surrounded by HTML. A separate script that outputs the data of a resized image would be more appropriately handled by a separate script. (Perhaps one that takes GET params for the size?) If you are trying to save a resized copy of the image instead of the original-size image, then you should probably do that after move_uploaded_file. You could probably use SimpleImage::save() to do that. Maybe this would work:

if(move_uploaded_file($_FILES['albumCover']['tmp_name'], $target)) 
{ 
    require_once 'SimpleImage.php';
    $image = new SimpleImage();
    $image->load($target);
    $image->resize(150,150);
    $image->save($target); // Overwrites the image with a resized one

    // Tells you if its all ok 
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else ...

Upvotes: 0

Tim Fountain
Tim Fountain

Reputation: 33148

At first glance, your image resizing code is inside the if( isset($_POST['submit']) ) { check on your form, but this code will never be run as the form submits to submitAlbumForm.php which is a different page entirely. I think you just need to move your resizing code into submitAlbumForm.php, probably inside the if(move_uploaded_file(...) block and change the paths as appropriate.

Upvotes: 1

Related Questions