martinm92
07-09-2012, 02: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
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