It all works fine, except I want the picture to be displayed on the same page as the form, in a “div” a “td” a iframe or something, not on a new blank side.
Can this be done with som smart code?
Sorry if I am asking for something that's already been discussed, but I could not find such a topic
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
As mentioned I am not much of a programmer and have therefor cut and pasted my code form a tutorial. The code looks like this:
<?php
if ($_POST) {
// print "<pre>";print_r($_FILES);print"</pre>";
// No image?
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
die(header("Location:imageresizer.php?error=1"));
}
$imagepath = $_FILES['image']['tmp_name'];
// Opening any kind of image
function open_image ($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }
# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }
return false;
}
// Load image
$image = open_image($imagepath);
if ($image == false) {
die(header("Location:imageresizer.php?error=2"));
}
// TODO: resizing the image
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
// Percentage?
if (!empty($_POST['percent']) AND is_numeric($_POST['percent'])) {
$percent = floatval($_POST['percent']);
$percent = $percent/100;
$new_width = $width * $percent;
$new_height = $height * $percent;
// New width? Calculate new height
} elseif (!empty($_POST['new_width']) AND is_numeric($_POST['new_width'])) {
$new_width = floatval($_POST['new_width']);
$new_height = $height * ($new_width/$width);
// New height? Calculate new width
} elseif (!empty($_POST['new_height']) AND is_numeric($_POST['new_height'])) {
$new_height = floatval($_POST['new_height']);
$new_width = $width * ($new_height/$height);
/* New height and new width
} elseif (!empty($_POST['height']) AND is_numeric($_POST['height']) AND !empty($_POST['width']) AND is_numeric($_POST['width'])) {
$new_height = floatval($_POST['height']);
$new_width = floatval($_POST['width']);
*/
}else {
die(header("Location:imageresizer.php?error=3"));
}
// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Display resized image
//header('Content-type: image/jpeg; location: $PHP_SELF');
//imagejpeg($image_resized, '../userfiles/image/'.$_FILES['image']['name']); // Laster opp fila med det lokale navn
//imagejpeg($image_resized); // Viser fila på skjerm
//die();
}
?>
I am calling it from another php-page like you recomended, (<pre><img src='imageresizer.php' alt='' /></pre>) but only get a blank or red cross result.
I wonder if you would be nice and look into it for me?
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Yes. I've been there earlier. And it's not what I'm looking for, as you will understand from my first post. I want the image to be shown in the index-file, where the upload form is.
I know I reach the imageresizer.php, because when I output the array content (as you can see now on my page), it changes evertime I'm choosing a new o\picture ... and there is no way that can be done axcept from inside imageresizer.php, is it?
The problem still remains; the picture will not appear ....
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
svein wrote:I forgot to remove the action='imageresizer.php' in the Form-tag before posting it to you. I did this just to test it again.
Inorder to do what you want, you need to rewrite most of what you have, the reason being php can not output both text (html) and an image at once.
They have to be 2 separate requests, this is why i said to use something like image.php?name=keyboard.
You index.php file never does anything with the uploaded files, which is what imageresizer.php is for, but then imageresizer.php can only out put that image, it can't do both.
That being said, the easiest way i see for you to write this is to add an iframe were you want the image to be shown, and have
Next problem wil be to upload this image to a given url, but I got some pretty good clues on how to do that. ... Hope you don't mind me comming back if I experience any problems with that one too ...
Now my script works fine with previewing an image, resizing it and all that, but ..
I cant find a way to save the pic using a different button. Now the picture is automatically uploaded to the right subfolder and that might just be an ok solution, but I really wish to let the users first resize, then upload (not the other way around).
I might ad that the script eventually will be used in an app that needs logon and were all pics are supposed to be put in the same folder, so that's the easy part ...
I have no problems turning on and off save. But I (of course) have no clue of how to make a [Save]-button function.