Trying to use the following scripts to upload, resize & save multiple images via a form. It's not working... not even stripped down to one input and foreach removed. I've tried combining the upload & resize scripts and then redirecting the form action to the combined script. Can't get that to work. Now trying to move the uploaded file from upload.php to SimpleImage.php using move_uploaded_file but it doesn't seem to be working either. Not getting any errors just blank pages. I will add better validation later, just trying to get it to work now. Sorry for so much code, just wanted to have all of it here for your reference.
My questions: How to I get upload.php to send the image to the resize script or should it be vise versa? any ideas on why it's not working or showing errors?
I'm new to php but am very determined to figure this out! I'll take any and all advice &/or insults you can throw at me! I learn from both! :) Thanks in advance!!!
the form:
<?php
if( isset($_POST['submit']) ) {
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['uploaded_image']['tmp_name']);
$image->resizeToWidth(500);
$image->save("uploads/uploadedfiles/".$pictures);
} else {
?>
<form action="/upload.php" method="post" enctype="multipart/form-data"></br>
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br />
<input type="file" name="uploaded_image[]" accept="image/gif, image/jpeg, image/png" /> <br /> <br />
<input type="hidden" name="MAX_FILE_SIZE" value="48000000" /> *Images must be no more than 5MB.<br /> .jpg, .gif & .png files accepted only.<br /><br />
<input type="submit" value="Upload"> </p> </form>
upload.php:
<?php
header("Location:confirmationpage.php");
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if (($_FILES["pictures"]["type"] == "image/gif")
|| ($_FILES["pictures"]["type"] == "image/jpeg")
|| ($_FILES["pictures"]["type"] == "image/png" )
&& ($_FILES["pictures"]["size"] < 50000))
{
move_uploaded_file(
$_FILES["pictures"]["tmp_name"][$key], "/SimpleImage.php" . $_FILES["pictures"]["name"][$key]);
}
}
?>
the resize script (SimpleImage.php):
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* 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($uploaded_image) {
$image_info = getimagesize($uploaded_image);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($uploaded_image);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($uploaded_image);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($uploaded_image);
}
}
function save($uploaded_image, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$uploaded_image,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$uploaded_image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$uploaded_image);
}
if( $permissions != null) {
chmod($uploaded_image,$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;
}
}
?>