Last few steps (hopefully)...
I found a resize script that seems to do the job - only it gives an error I cannot solve no matter how I write the string it needs. The bit that's causing the issue is listed first, the whole upload script second and the class I'm calling third (sorry for the acres of code but I thought it might help)
The issue (I think) is with the line: (in the first block)
$image->image_to_resize = "members/".$id."/images/".$newname;
which asks for a full path (which I can't give it because it could be anywhere) so I'm
giving it all the variables it needs to create the path. Only it doesn't...
The error I get is: (pasted - it really does say .jpg.jpg)
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'members/$id/images/14.jpg.jpg' for writing in /Sites/com/shutterbugclub/www/scripts/resize.class.php on line 140
All and any help much appreciated because once the resizing is done (I broke the mechanism that was working
to do this - God knows how but I did) then the whole thing can be wrapped up and made to look pretty.
Best wishes
Monty
CODE: The resize bit at the end of the upload script:
Code: Select all
$image = new Resize_Image;
$image->new_width = 900;
$image->new_height = 530;
$image->image_to_resize = "members/".$id."/images/".$newname; // Full Path to the file
$image->ratio = true; // Keep Aspect Ratio?
// Name of the new image (optional) - If it's not set a new will be added automatically
$image->new_image_name = $newname;
/* Path where the new image should be saved. If it's not set the script will output the image without saving it */
$image->save_folder = 'members/$id/images/';
$process = $image->resize();
if($process['result'] && $image->save_folder)
{
echo 'The new image ('.$process['new_file_path'].') has been saved.';
}
$success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
CODE: The whole upload script
Code: Select all
<?php
session_start();
if (@!$_SESSION['id']) {
$msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>';
include_once 'msgToUser.php';
exit();
}
//////////////////////////////////////////////// End member Log in check ///////////////////////////////////////////////////
// Connect to database, needed every time the page runs or is refreshed in a browser
include_once "scripts/connect_to_mysql.php";
include 'scripts/resize.class.php';
define ("MAX_SIZE","100");
// Get the user session id variable into a local php variable for easier use in scripting
$id = $_SESSION['id'];
// Now let's initialize vars to be printed to page in the HTML section so our script does not return errors
// they must be initialized in some server environments, not shown in video
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$error_msg = "";
$success_msg = "";
$firstname = "";
$lastname = "";
$user_pic = "";
// Parsing section for the member picture... only runs if they attempt to upload or replace a picture
if (@$_POST['parse_var'] == "pic"){
$image=$_FILES['fileField']['name'];
if ($image)
{
$filename = stripslashes($_FILES['fileField']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
}}
if (@!$_FILES['fileField']['tmp_name']) {
$error_msg = 'Please browse for an image before you press Go.';
} else {
$maxfilesize = 256000; // 256000 bytes equals 250kb
if($_FILES['fileField']['size'] > $maxfilesize ) {
$error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
unlink($_FILES['fileField']['tmp_name']);
} else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {
$error_msg = 'Ooops - Your image was not one of our accepted formats, please try again.';
unlink($_FILES['fileField']['tmp_name']);
} else {
// rename the picture incfrementally
$extension = getExtension($filename);
$extension = strtolower($extension);
$groovy = sizeof(glob("members/$id/images/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
$newname="".$image_name;
$place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
chmod ("members/$id/images/$newname", 0666);
print $newname;
$image = new Resize_Image;
$image->new_width = 900;
$image->new_height = 530;
$image->image_to_resize = "members/".$id."/images/".$newname; // Full Path to the file
$image->ratio = true; // Keep Aspect Ratio?
// Name of the new image (optional) - If it's not set a new will be added automatically
$image->new_image_name = $newname;
/* Path where the new image should be saved. If it's not set the script will output the image without saving it */
$image->save_folder = 'members/$id/images/';
$process = $image->resize();
if($process['result'] && $image->save_folder)
{
echo 'The new image ('.$process['new_file_path'].') has been saved.';
}
$success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
//NOW MAKE THE RESIZE CALL
//img_resize ($_FILES [ 'fileField'] [ 'name'], $_FILES [ 'fileField'] [ 'tmp_name'], 537, $newname);
}
Some MORE Code The resize class I'm calling as an include:
Code: Select all
<?php
/*
---------------------------------------------------------------------
Credits: Bit Repository
Source URL: http://www.bitrepository.com/resize-an- ... nd-gd.html
---------------------------------------------------------------------
*/
class Resize_Image {
var $image_to_resize;
var $new_width;
var $new_height;
var $ratio;
var $new_image_name;
var $save_folder;
function resize()
{
if(!file_exists($this->image_to_resize))
{
exit("File ".$this->image_to_resize." does not exist.");
}
$info = GetImageSize($this->image_to_resize);
if(empty($info))
{
exit("The file ".$this->image_to_resize." doesn't seem to be an image.");
}
$width = $info[0];
$height = $info[1];
$mime = $info['mime'];
/*
Keep Aspect Ratio?
Improved, thanks to Larry
*/
if($this->ratio)
{
// if preserving the ratio, only new width or new height
// is used in the computation. if both
// are set, use width
if (isset($this->new_width))
{
$factor = (float)$this->new_width / (float)$width;
$this->new_height = $factor * $height;
}
else if (isset($this->new_height))
{
$factor = (float)$this->new_height / (float)$height;
$this->new_width = $factor * $width;
}
else
exit("neither new height or new width has been set");
}
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type)
{
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
// New Image
$image_c = ImageCreateTrueColor($this->new_width, $this->new_height);
$new_image = $image_create_func($this->image_to_resize);
ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height);
if($this->save_folder)
{
if($this->new_image_name)
{
$new_name = $this->new_image_name.'.'.$new_image_ext;
}
else
{
$new_name = $this->new_thumb_name( basename($this->image_to_resize) ).'_resized.'.$new_image_ext;
}
$save_path = $this->save_folder.$new_name;
}
else
{
/* Show the image without saving it to a folder */
header("Content-Type: ".$mime);
$image_save_func($image_c);
$save_path = '';
}
$process = $image_save_func($image_c, $save_path);
return array('result' => $process, 'new_file_path' => $save_path);
}
function new_thumb_name($filename)
{
$string = trim($filename);
$string = strtolower($string);
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
$string = ereg_replace("[ tnr]+", "_", $string);
$string = str_replace(" ", '_', $string);
$string = ereg_replace("[ _]+", "_", $string);
return $string;
}
}
?>