Jump to content

another damn image resize thread!


delphi123

Recommended Posts

Ok I've tried really hard not to post this as I know there always seems to be folk struggling with this and you guys are probably sick of it!

However I've read the posts, googled, etc and I'm just struggling to translate the scripts to my app (most seem to be doing TOO much which confuses my fragile mind!)

 

Ok, here's my complete code, which I've made with some help from the forum brains here:

<?php
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";

//SET THE DIRECTORY NAME TO UPLOAD TO
$y = date('Y');
$m = date('M');
$path_to_images = 'images/';
if(!is_dir($path_to_images.$y)){
mkdir($path_to_images.$y);
}
if(!is_dir($path_to_images.$y.'/'.$m)){	
mkdir($path_to_images.$y.'/'.$m);
}
$upload_to = $path_to_images.$y.'/'.$m.'/';//this is the folder that you need to upload to

$imagename = basename($_FILES['image_file']['name']);

//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = $upload_to . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);

if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>


<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?php $_SERVER["PHP_SELF"];?>">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>

<?php
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

 

Then I've found the below script that sort of does what I need.

 

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

 

 

I really need integrate the above script so if it's resized on upload to 292px x 176px, then create a thumbnail of 147px x 89px exactly.

 

The variable $newimage should have the full source path and I guess $result must contain the server location as in:

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);

 

Can anyone give me a hand in integrating this feature - I've read through the http://uk.php.net/imagecopyresized bit but I can't get my head around how these images are saved?  Does it do this automatically?

 

Many thanks folks!

Link to comment
https://forums.phpfreaks.com/topic/83503-another-damn-image-resize-thread/
Share on other sites

Man my codes a complete mess now, I've tried banging these two together, but it just doesn't work!

 

I figure this is where I need the script:

if(empty($error))
{
$newimage = $upload_to . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);

if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

Ok I reckon this 'should' work, except I'm getting the error:

 

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'images/2007/Dec/images/2007/Dec/DSCF3951.JPG' for writing: No such file or directory in F:\Wamp Virtual Server\!Virtual Server\CG2\dev\image_upload\upload.php on line 50

 

Now it 'is' creating the file and uploading it to the correct directory, so I can't figure out what's wrong?

 

any geniuses about today?

 

<?php
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";

//SET THE DIRECTORY NAME TO UPLOAD TO
$y = date('Y');
$m = date('M');
$path_to_images = 'images/';
if(!is_dir($path_to_images.$y)){
mkdir($path_to_images.$y);
}
if(!is_dir($path_to_images.$y.'/'.$m)){	
mkdir($path_to_images.$y.'/'.$m);
}
$upload_to = $path_to_images.$y.'/'.$m.'/';//this is the folder that you need to upload to


// SET THE NAME OF THE IMAGE FILE
$imagename = basename($_FILES['image_file']['name']);

//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = $upload_to . $imagename;
//echo $newimage;

//IMAGE RESIZE SCRIPT START
$uploadedfile = $_FILES['image_file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newwidth=292;
$newheight=176;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $_FILES['uploadfile']['name'];
echo $newimage;
imagejpeg($tmp,$upload_to.$newimage,100);
imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
//IMAGE RESIZE SCRIPT FINISH

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);

if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>


<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?php $_SERVER["PHP_SELF"];?>">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>

<?php
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.