MY PHP QUESTION
When I try to upload a .GIF/.gif image using the codes below I get the error:
Warning: imagecreatefromgif(): 'images/30146mp3.gif' is not a valid GIF file in /server/www/upload.php on line 88
Warning: imagesx(): supplied argument is not a valid Image resource
in /server/www/upload.php on line 91
Warning: imagesy(): supplied argument is not a valid Image resource
in /server/www/imgloader_up.php on line 92
Warning: imagecopyresampled(): supplied argument is not a valid Image resource
in /server/www/upload.php on line 107
Warning: imagedestroy(): supplied argument is not a valid Image resource
in /server/www/upload.php on line 122
Here are the codes.
<?
include "config.php";
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">File is too big !</font><br>\n"; exit; }
if(($HTTP_POST_FILES['userfile']['type']=="image/gif") ||
($HTTP_POST_FILES['userfile']['type']=="image/GIF") ||($HTTP_POST_FILES['userfile']['type']=="image/JPG") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {
if (file_exists("./".$path . $HTTP_POST_FILES['userfile']['name'])) {
echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">There already exists a file with this name, please rename your file and try again</font><br>\n"; exit; }
//generate random number
$zufall = rand(1,99999);
$fupl = "$zufall";
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl .$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else {
?>
<br>
<?
//set url variable
$domst = "";
$drecks = "/";
$imgf = $fupl.$HTTP_POST_FILES['userfile']['name'];
$thbf = $tpath.$imgf;
$urlf = $domst .$domain .$drecks .$path .$imgf;
//create thumbnails
function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg|JPG/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png|PNG/',$system[1])){
$src_img=imagecreatefrompng($name);
}
if (preg_match('/gif|GIF/',$system[1])){
$src_img=imagecreatefromgif($name);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
}
if (preg_match("/gif/",$system[1]))
{
imagegif($dst_img,$filename);
}
else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
createthumb($path.$imgf,$tpath.$imgf,$tsize,$tsize);
?>
<body bgcolor="#ffffff">
<div class="support" id="agreement">
<table border='0' >
<link rel="STYLESHEET" type="text/css" href="style.css">
<FORM action="nowhere" method="post">
<tr><td>DIRECT URL:</td><td><input type="text" name="thetext" size="60" value="<? echo $urlf; ?>"></td></tr>
<tr><td>HTML:</td><td><input type="text" name="thetext" size="60" value="<img src='<? echo $urlf; ?>'>"></td></tr>
<tr><td>HTML (LINK):</td><td><input type="text" name="thetext" size="60" value="<a href='URL'><img src='<? echo $urlf; ?>'></a>"></td></tr>
<tr><td>IMAGE:</td><td> <a href="<? echo $urlf; ?>" target="_blank"><img src="<? echo $urlf; ?>" border="0" width="300" height="300"></a> <br>(Click to Enlarge) </td></br><a href="javascript:history.go(-1);" title="go back to previous page">Back</a> </div></tr></td></tr>
<?
}
} else { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\"><link rel='stylesheet' href='style.css' type='text/css'><div class='support' id='agreement'><p><center>You selected a wrong filetype!</font></center></div><br>\n"; exit; }
}
?>
I am wondering if there is a way to stop the error, but still generate thumbnails for GIFS, or to remove the code, and generate thumbnails for all other images, thanks.
If you want to test it the config file includes:
<?
//Edit this part!
$domain = "http://www.site.com/directory";
//your domain, eg "http://www.youdomain.com", or "http://www.youdomain.com/upload" no ending slash! //
//File size etc
$max_size = 2048000; //max. allowed size (KB) USE 2048000
$tsize = "300"; //thumbnails size (pixel)
$path = "http://www.webmaster-talk.com/images/"; //your image path, where the images should be uploaded to
$tpath = "http://www.webmaster-talk.com/images/thumbnails/"; //your thumbnails path
?>
The image uploads to the directory but the thumbnail wont work.