/    Sign up×
Community /Pin to ProfileBookmark

Upload And Thumbnail

Hello.
I wrote a script to upload images and then preview a thumbnail.
My upload form is in a seperate file called upload_form.inc

Everything worked until I tried to add support for gif and png in the [I]thumbnail function[/I]!

Please, what is wrong? is it something to do with the way I nested the if statement?

[code=php]<html>

<head>
<title>Upload</title>
</head>

<body>
<?php
if(!isset($_POST[‘Upload’]))
{
include(“upload_form.inc”);
exit();
}/*endif*/
else
{
if($_FILES[‘pix’][‘tmp_name’] == “none”)
{
echo “<b>File did not successfully upload. Check the file size. File must be less than 1Mb.<br>”;
include(“upload_form.inc”);
exit();
}
if(!ereg(“image”,$_FILES[‘pix’][‘type’]))
{
echo “<b>File is not a picture. Please try another file.</b><br>”;
include(“upload_form.inc”);
exit();
}
else
{
function thumbnail($image_path,$thumb_path,$image_name,$thumb_width)
{
if(ereg(“image/pjpeg”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefromjpeg(“$image_name”);
}
elseif(ereg(“image/gif”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefromgif(“$image_name”);
}
elseif(ereg(“image/png”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefrompng(“$image_name”);
}
else
{
$src_img = imagecreatefromjpeg(“$image_name”);
}
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$ratio=$thumb_width/$origw;
$new_h=$origh * $ratio;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));

imagejpeg($dst_img, “$thumb_path/$image_name”);
return true;
}
}
$ran = rand (1, 100000) ;
$destination = $firstname.””.$ran.”.”.$_FILES[‘pix’][‘name’];
$temp_file = $_FILES[‘pix’][‘tmp_name’];
move_uploaded_file($temp_file,$destination);
$imgpath = “http://www.mywebsite.ch/rwt/junk/UPLOAD%20AND%20SEND/{$ran}.{$_FILES[‘pix’][‘name’]}”;
echo “<p><b>Your image:</b></p>”;
thumbnail(0,”thumbs”,”{$ran}.{$_FILES[‘pix’][‘name’]}”,200);
echo “<a href=’$imgpath’ title=’$firstname portrait’ target=’_blank’ alt=’$firstname portrait><img src=’http://www.mywebsite.ch/rwt/junk/UPLOAD%20AND%20SEND/thumbs/{$ran}.{$_FILES[‘pix’][‘name’]}’ style=’border: 1px solid #C0C0C0′ alt=’$firstname’></a>”;
}
?>

</body>

</html>[/code]

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaJun 21.2007 — When you say everything worked till you added support for gif and png so did .jpg files get uploaded correctly ?
Copy linkTweet thisAlerts:
@rwtauthorJun 21.2007 — well when I added the following lignes in the function:
[code=php] if(ereg("image/pjpeg",$_FILES['pix']['type']))
{
$src_img = imagecreatefromjpeg("$image_name");
}
elseif(ereg("image/gif",$_FILES['pix']['type']))
{
$src_img = imagecreatefromgif("$image_name");
}
elseif(ereg("image/png",$_FILES['pix']['type']))
{
$src_img = imagecreatefrompng("$image_name");
}
else
{
$src_img = imagecreatefromjpeg("$image_name");
}[/code]

There was no preview anymore with jpeg images. And for gif and png, it just gets an error: [I]'11988.tick-green.png' is not a valid JPEG file [/I].

So my statement does not work.

Try it directly on server: http://www.allan.ch/rwt/junk/UPLOAD%20AND%20SEND/upload.php

thanks
Copy linkTweet thisAlerts:
@rwtauthorJun 21.2007 — 
  • - DING - could anyone help?
  • Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — Try using an ereg on just jpeg, png, gif.. not image. Also, for debugging, add an Echo after each $src_image and say something like "got passed setting SRC_IMG", to see if the if statements are picking up. If you don't see the "got passed setting src_img" then you know the if statement isn't passing criteria.

    Also you could just echo out the $_FILES['pix']['type'] to see what the file type is.

    Peace
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — Okay, thanks.

    It passes the statements, so they work.

    I also added print_r($_FILES['pix']['type']); so that it would print out the type.

    But the script still stops though the thumb is created.

    I think its my echo img.

    please help
    Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — ...... I think i found your problem:

    -- edit, nvm --
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — Now I tried removing the image type check and it still doesn't work.... so it actually means that its something else.
    Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — Is it making the image file, and transferring it?
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — ...... I think i found your problem:

    -- edit, nvm --[/QUOTE]


    actually $destination was just meant to be there if I wanted to echo the image path.

    But I tried to change it and it still doesn't work.

    But if there's an array after the variable, do I still need to keep the ."". ?

    thanks
    Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — ok, it works for me. I think this is your problem. You have a space in your destination url. Get rid of the spaces. and change the dir name.
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — well the function creates the resized image and them I just wanna display it. Thats all.
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — ok, it works for me. I think this is your problem. You have a space in your destination url. Get rid of the spaces. and change the dir name.[/QUOTE]

    I'm sorry, I don't follow, were is the space?
    Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — ok, I think I got it this time. if you notice on the line that contains the link and image. at the ALT for the link, you didn't end it. you just have alt='$firstname portrait

    and thats it.. no ending '.. so add that and see what it gives ya ?.
    Copy linkTweet thisAlerts:
    @rwtauthorJun 21.2007 — Haaa! man.... I'm sooo stupid.

    Haha, thanks for helping me out pal.

    And now it works with the if statement for the file type!

    thanks again
    Copy linkTweet thisAlerts:
    @Kyleva2204Jun 21.2007 — ? no prob. we all make small errors like that.
    ×

    Success!

    Help @rwt spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...