...

View Full Version : Image resizer



martinm92
07-09-2012, 03:17 PM
Hey guys, I have a little script for image re sizing, getting all kind of errors when I run it.

The uploader works fine on its own, but when i include the re sizer, it will still upload my original image but doesn't re size the other images.

Here's my form and upload script:


<form name="uploader" method="POST" enctype="multipart/form-data">
<p>Image Name:</p>
<p><input type="text" name="title" class="imageUploader" /></p>
<p>Image Description:</p>
<p><textarea name="message" class="upload_desc"></textarea></p>
<p>File Name:</p>
<p><input type="file" name="file" class="image_upload" /></p>
<p><input type="submit" value="Upload" class="upload_submit" /></p>
</form>




if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(($_FILES['file']['type'] == 'image/gif')
|| ($_FILES['file']['type'] == 'image/jpeg')
|| ($_FILES['file']['type'] == 'image/png')
|| ($_FILES['file']['type'] == 'image/pjpeg')){
if($_FILES['type']['error'] > 0){
echo "Return Code: " . $_FILES['file']['error'] . "<br>";
}else{
$title = $_POST['title'];
$desc = $_POST['message'];
$original = $_FILES['file']['name'];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

$newfiles = md5(time()) . "." . $ext;

if(move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$original)){
mysql_query("INSERT INTO `img` (`title`, `desc`, `img_s`, `img_m`, `img_org`) VALUES ('".$title."', '".$desc."', '".$newfiles."', '".$newfiles."', '".$original."')");
include("image_resize.php");
echo "<p>Upload successful!</p>";
}else{
echo "<p>Failed to upload!</p>";
}
}
}else{
echo "<p>Invalid file type!</p>";
}
}


Here is the image re sizer:


function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
$s_path = trim($s_path);
$o_path = trim($o_path);
$save = $s_path . $save;
$file = $o_path . $file;
$ext = strtolower(end(explode('.',$save)));
list($width, $height) = getimagesize($file) ;
if(($width>$t_w) OR ($height>$t_h)) {
$r1 = $t_w/$width;
$r2 = $t_h/$height;
if($r1<$r2) {
$size = $t_w/$width;
}else{
$size = $t_h/$height;
}
}else{
$size=1;
}
$modwidth = $width * $size;
$modheight = $height * $size;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
switch ($ext) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($file) ;
break;
case 'gif':
$image = imagecreatefromgif($file) ;
break;
case 'png':
$image = imagecreatefrompng($file) ;
break;
}
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
return;
}

$file = $newfiles;
$filename = $dir . $file;

/* Small Picture */
$save = $filename;
$t_w = 150;
$t_h = 150;
$o_path = "upload/";
$s_path = "upload/small/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

/* Photogallery Picture */
$t_w = 250;
$t_h = 165;
$o_path = "upload/";
$s_path = "upload/medium/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);


Just some of the errors if you want to see them:

Warning: getimagesize(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 8

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\admin\image_resize.php on line 22

Warning: imagecreatefromjpeg(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 26

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 35

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 36

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\admin\image_resize.php on line 7

Warning: getimagesize(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 8

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\admin\image_resize.php on line 22

Warning: imagecreatefromjpeg(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 26

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 35

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 36

DrDOS
07-09-2012, 04:43 PM
Those scripts are not written to work together, so tacking the second one on just won't cut it. And to me it's not a 'little script', there's a lot more there than is needed.

For the Resize_Image function, I see that none of it's parameters are defined in the first script, so it can't find $file, which it needs in the first step, so all the others fall like dominoes. I don't use GD, I use Imagemagick, so I don't have a ready made script for you.

martinm92
07-09-2012, 04:56 PM
Ah ok, so can I edit this script to work with my uploader?

tangoforce
07-09-2012, 05:33 PM
The number of errors you have listed there is going to put off anyone from assisting here really.

You've also posted the function and the other php code but not told us which one of each is in what file name. How do I know what image_resize.php on line 8 is referring to? - The function code or the php code above it?

To be honest, I think you're going to be better off dumping that code and using this from white-hat-web-design in the UK:



<?
/*
* 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=100, $permissions=null)
{
switch ($image_type)
{
case IMAGETYPE_JPEG:
case 'image/jpeg':
imagejpeg($this->image,$filename,$compression);
break;
case IMAGETYPE_GIF:
case 'image/gif':
imagegif($this->image,$filename);
break;
case IMAGETYPE_BMP:
case 'image/bmp':
imagewbmp($this->image,$filename);
break;
}

if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG)
{
switch ($image_type)
{
case IMAGETYPE_JPEG:
case 'image/jpeg':
imagejpeg($this->image);
break;
case IMAGETYPE_GIF:
case 'image/gif':
imagegif($this->image);
break;
case IMAGETYPE_BMP:
case 'image/bmp':
imagewbmp($this->image);
break;
}
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum