View Full Version : Uploading / Resizing Images?
mada9369
01-17-2009, 03:20 AM
Am i calling to the function wrong?
I cant find another reason why this wouldnt work...
<?php
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if(isset($_POST['image'])){
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if($extension == "png"){
function uploadImage($percent, $n){
global $extension;
global $filename;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrompng($filename);
$image_name=md5(time()).'.'.$extension;
$newname="images/$n_".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
}
}
uploadImage(1, l);
uploadImage(0.60, m);
uploadImage(0.35, s);
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" id="image" />
<input type="submit" value="Upload image" id="image" />
</form>
JasonDFR
01-17-2009, 09:08 AM
Always give name attributes to your form inputs.
<input type="file" name="image" />
<input type="submit" value="Upload image" name="submit" />
You cannot have two elements with the same ID.
Also, it is always better to test for the form being submitted than to test for any element. So instead of:
if(isset($_POST['image'])){
I would put:
if ( isset($_POST['submit']) ) {
Doing this forces someone to submit the form rather than just posting an image to your script.
JasonDFR
01-17-2009, 09:15 AM
The code below may be easier to use than the function in your code:
$file_name = basename( $_FILES['image']['name'] ); // File name ex: 'image.jpg'
$ext = substr( $file_name, strrpos($file_name, '.') + 1 ); // File extension ex 'jpg'
Good Luck,
J
mada9369
01-17-2009, 09:39 PM
Always give name attributes to your form inputs.
<input type="file" name="image" />
<input type="submit" value="Upload image" name="submit" />
You cannot have two elements with the same ID.
Also, it is always better to test for the form being submitted than to test for any element. So instead of:
if(isset($_POST['image'])){
I would put:
if ( isset($_POST['submit']) ) {
Doing this forces someone to submit the form rather than just posting an image to your script.
That worked perfectly in sending the information!
But when i try to upload an image; it gives errors...
It says that there is something wrong with the getimagesize(); function.
list($width, $height, $type, $attr) = getimagesize($filename);
JasonDFR
01-18-2009, 06:29 AM
Copy and paste your error or link the page with the error.
I'm glad the first part is working for you.
Jason
JasonDFR
01-18-2009, 07:37 AM
The ['name'] index of the $_FILES array only holds the original file name of the file the user uploaded, AS IT EXISTS ON THE USERS MACHINE.
You need to use the ['tmp_name'] index to get the actual file location where PHP has copied the user's file to ON YOUR WEB SERVER.
So,
$tmp_name = $_FILES['image'] ['tmp_name'];
list($width, $height, $type, $attr) = getimagesize($tmp_name);
should work. Useing the ['name'] index with getimagesize() will never work.
There are some other issues with your code:
Define functions before you start coding. A function definition should never be inside an IF block.
You are going to have the same problem with $source = imagecreatefrompng($filename); You need to use the ['tmp_name'] when working with the uploaded file.
You are eventually going to have to use move_uploaded_file("file", "dir/name.ext") to put your files in their final storage place after you are done with your resizing.
Since you are making numerous resized images, you'll need to think about how to store them without losing the original ['tmp_name'] file.
If this script is going to be used by anyone other than you, you'll want to think about security too.
Good luck,
Jason
mada9369
01-18-2009, 06:37 PM
$tmp_name = $_FILES['image'] ['tmp_name'];
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrompng($tmp_name);
$image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$image_name=md5(time()).'.'.$extension;
$newname="images/$name".$image_name;
$copied = copy($image, $newname);
this is what i was planning on doing for moving and resizing the images.
but it gives an error
Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34
Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34
Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34
mada9369
01-19-2009, 06:59 PM
Maybe there is a way to convert
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); into something else besides 1 or 0?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.