View Full Version : Frustration with image resizer working and not working depending on filename?

03-08-2009, 06:50 AM
Hello again. *sighs*

After scouring the web for help on image-resizing scripts, I thought I had figured out what I needed to get the job done. Everything was going marvelous until I started trying to upload random pictures (via testing on my local machine, mind you) with the script.

Some would work, some would not.

Basically, its a news posting script.
-- member selects a picture for the story, inputs a headline and the article text.
-- If no image is chosen, a default one will be selected.
-- If there IS an image chosen, it will be uploaded and resized down to 500 pixels wide AND create another 100pixel thumbnail of the same image.

It was running just fine until I started messing around and selecting different images. The test images I had been using were not necessarily small, mind you, but they weren't uncompressed .JPG's from digital cameras. I'm not sure if the filesize has anything to do with it (as I'm not setting a max-file-size in the upload form...or should I?) because whenever I try to upload a 2MB+ image the script just jumps to the section where it will insert the information and use a default image, instead of doing what it is supposed to do.

Excuse the frankenstein-like coding below, which was taken and modified from a script found on the net. I'm just half baffled as to what is going on, because what I would like is for my team members to be able to take a screenshot of our wonderful racing simulation and upload it for a news story. Generally depending on the filetype used in FRAPS (screen capture program that is popular in our circle) the filesizes can range.

I'm just...lost as to what is going on. I figured at the start it was the spaces in the filename such as "World Series 001.jpg" not working, where something like "iRacingScreenshot.jpg" would. But then I removed the spaces and renamed the files opposite, and they worked just fine.

Confusion. :(




// define the article variables.
$userID = $_SESSION['userID'];
$headline = addslashes($_POST['headline']);
$article = addslashes($_POST['article']);
$date = time();

//make sure this directory is writable!
$path_thumbs = "../newsimages/";

//the new width of the resized images, in pixels.
$img_thumb_width = 500;
$img_thumb_width2 = 110; //

//Limit allowed extensions? (no for all extensions allowed)
$extlimit = "yes";

//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

//the image -> variables
$file_type = $_FILES['vImage']['type'];
$file_name = $_FILES['vImage']['name'];
$file_size = $_FILES['vImage']['size'];
$file_tmp = $_FILES['vImage']['tmp_name'];

//check if you have selected a file
$imageFilename = "newsDefault.jpg";

$insertNonPicNews = mysql_query("INSERT INTO news (userID, date, headline, article,imageFilename) VALUES ('$userID','$date','$headline','$article','$imageFilename')");

if ($insertNonPicNews == TRUE)
echo "You did not select a picture to upload with your news article. All of the information was entered accordingly, and a default image will be shown for the article.";
exit(); //exit the script and don't process the rest of it!
else {
echo "Ryan screwed up the coding somewhere. Ugh. :(";
exit(); //exit the script and don't process the rest of it!


//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);

//uh-oh! the file extension is not allowed!
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

//so, whats the file's extension?
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);

//the new width variable
$ThumbWidth = $img_thumb_width;
$ThumbWidth2 = $img_thumb_width2;




//keep image type

if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);

//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);
//calculate the image ratio


if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;

$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;

//calculate the thumb ratio


if ($imgratio>1){
$newwidth2 = $ThumbWidth2;
$newheight2 = $ThumbWidth2/$imgratio2;
$newheight2 = $ThumbWidth2;
$newwidth2 = $ThumbWidth2*$imgratio2;

//function for resize image.

if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
die("Error: Please make sure you have GD library ver 2+");

//function for resize thumb.
if (function_exists(imagecreatetruecolor)){
$resized_img2 = imagecreatetruecolor($newwidth2,$newheight2);
die("Error: Please make sure you have GD library ver 2+");

//the resizing is going on here!
imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//the resizing is going on here!
imagecopyresampled($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);

//finally, save the image
imagejpeg($resized_img,"$path_thumbs/$rand_name.$file_ext", 90);


// the thumb
$thumbext = "_thumb";
imagejpeg($resized_img2,"$path_thumbs/$rand_name$thumbext.$file_ext", 100);




//ok copy the finished file to the thumbnail directory
if(move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext")) {

$imageFilename = $rand_name.".".$file_ext;
$insertPicNews = mysql_query("INSERT INTO news (userID, date, headline, article,imageFilename) VALUES ('$userID','$date','$headline','$article','$imageFilename')");

if ($insertPicNews == TRUE) {

//Tells you if its all ok
echo "The picture and article have been added to our database. You are now being redirected back to the uploader.";
echo "<meta http-equiv='refresh' content='3;url=newsPublisher.php' />";
else {
echo "There was a problem with Ryan's goddamn script!";
else {
echo "There was a problem with the process. Ryan is probably the idiot who coded it incorrectly! :( ";

// Echo the form
<form action="<?php $_SERVER[PHP_SELF]; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<p><input type="file" name="vImage" /> &laquo; Select the image you want to be displayed with the news story (Not needed, but recommended. Please have no spaces in the filename!)</p>
<p><input name="headline" type="text" size="25" maxlength="90" /> &laquo; Article Headline (try to keep it to 90 letters or less. Something catchy.</p>
News Article Text: (No limit, be as long or short as you want.)<br />
<textarea name="article" cols="100" rows="7"></textarea>
<p><input type="Submit" name="Submit" value="Submit" /></p>


03-08-2009, 07:37 AM
Try a Google for "php upload size limit". My first result: http://www.radinks.com/upload/config.php

03-08-2009, 08:36 AM
I increased it, but am still encountering the same problem.

Perhaps my coding is out of order, or some such?

Or am I possibly testing the wrong variables, and doing operations based off of those incorrect variables?

03-08-2009, 09:29 AM
The code has no logic to check that the upload worked before accessing any of the uploaded file information or data - http://us.php.net/manual/en/features.file-upload.errors.php

All code must have error checking (check if something worked or didn't), error reporting (output a meaningful user message about what happened that will prevent the script from performing its intended function and log the error so you have a record of problems that you will need to find and fix), and error recovery logic (what do you do when there is an error, blindly continue executing using nonexistent data or gracefully stop.)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum