Jump to content

Recommended Posts

Hello.

I created an upload form with then creates a thumbnail.

 

The I got to a point were everything seemed to work fine exept that is was only for JPEG image types.

So I tried adding in the thumbnail function an if statement to detect what filetype it was. But then the jpeg don't show anything and gif/png get an error message!

 

 

<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> 

 

Whats the problem? Is it something to do with the braquets?

 

If you want to try it out, click here

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/56565-solved-thumbnail-from-image-upload/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.