Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Recommended Posts

Using GD to convert uploaded files into two seperate files of different size (one a thumbnail).

 

Random images are saved as black.  This may be just the thumbnail, just the larger image or both.

 

It seems if an image does work successfully, it always will in the future, it just seems to be random images.

 

I am using JPG - PNG - GIF, and have the same results with all.

 

The file does exist and does have a size in KB.

 

It seems by googling that this does happen, but I cannot find any help as to how to sort it out.

 

Advice appreciated!

Link to comment
https://forums.phpfreaks.com/topic/152808-php-gd-random-black-images/
Share on other sites

;D  I guess.

 

 

<?php

$ThumbWidth = 250;
$ImageWidth = 1000;
$maxsize = 700000;

$albumname = $_POST["albumname"];
$albumdescription = $_POST["albumdescription"];
$albumsecurity = $_POST["albumsecurity"];
$image1title = $_POST["image1title"];
$image1description = $_POST["image1description"];
$image2title = $_POST["image2title"];
$image2description = $_POST["image2description"];
$image3title = $_POST["image3title"];
$image3description = $_POST["image3description"];
$image4title = $_POST["image4title"];
$image4description = $_POST["image4description"];
$image5title = $_POST["image5title"];
$image5description = $_POST["image5description"];
$username = $_POST["username"];
$viewuser = $_POST["viewuser"];
$formsession = $_POST["formsession"];

if (empty($username) ){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl";
header( $redirect ) ;
}

$filename1 = time();

if (!file_exists("./images/$viewuser")){
mkdir("./images/$viewuser", 0755); 
mkdir("./images/$viewuser/thumbs", 0755);
}

$imagespath = "./images/$viewuser";

//Upload Images
$fileloop = 1;
$timestamp = time();
$filename = $timestamp;

while ($fileloop <= 5){
  if ($_FILES["image$fileloop"]["name"] != "") {
    if ($_FILES["image$fileloop"]["size"] > $maxsize) { $filestatus[$fileloop] = "filetoolarge"; }  
  	if ($_FILES["image$fileloop"]["error"] > 0) { $filestatus[$fileloop] = "failed"; } 
  	$filenameext = substr($_FILES["image$fileloop"]["name"], strrpos($_FILES["image$fileloop"]["name"], '.') + 1);
  $filenameextlower = strtolower($filenameext);
  if (($filenameextlower != "jpg") && ($filenameextlower != "jpeg") && ($filenameextlower != "png") && ($filenameextlower != "gif")) {$filestatus[$fileloop] = "incorrectfile";}
  if (($filestatus[$fileloop] != "failed") && ($filestatus[$fileloop] != "filetoolarge") && ($filestatus[$fileloop] != "incorrectfile")){

    $fullfilename[$fileloop] = "$filename.$filenameextlower";
    $target = "$imagespath/$fullfilename[$fileloop]";
	  $targetth = "$imagespath/thumbs/$fullfilename[$fileloop]";
	  $tmpfile = $_FILES["image$fileloop"]["tmp_name"];
	  $filetype = $_FILES["image$fileloop"]["type"];

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

      list($width, $height) = getimagesize($tmpfile);
      $imgratio=$width/$height;

	  //Decide if to resize and save thumb
	  if ($width > $ThumbWidth){
        if ($imgratio>1){
          $newthumbwidth = $ThumbWidth;
          $newthumbheight = $ThumbWidth/$imgratio;
        }else{
          $newthumbheight = $ThumbWidth;
          $newthumbwidth = $ThumbWidth*$imgratio;
        }
        $resized_thumb = imagecreatetruecolor($newthumbwidth,$newthumbheight);
        imagecopyresized($resized_thumb, $new_img1, 0, 0, 0, 0, $newthumbwidth, $newthumbheight, $width, $height);
        if(ImageJpeg ($resized_thumb,$targetth)) {$thumbstatus = 1;}
      }else{
	    if(copy($tmpfile, $targetth)) {$thumbstatus = 1;}
	  }

      //Decide if to resize and save image
	  if ($width > $ImageWidth){
        if ($imgratio>1){
          $newimagewidth = $ImageWidth;
          $newimageheight = $ImageWidth/$imgratio;
        }else{
          $newimageheight = $ImageWidth;
          $newimagewidth = $ImageWidth*$imgratio;
        }
        $resized_image = imagecreatetruecolor($newimagewidth,$newimageheight);
        imagecopyresized($resized_image, $new_img1, 0, 0, 0, 0, $newimagewidth, $newimageheight, $width, $height);
		  if(ImageJpeg($resized_image,$target)) {$imagestatus = 1;}
	  }else{
	    if(move_uploaded_file($tmpfile, $target)) {$imagestatus = 1;}
  	}
	  //
	  if ($imagestatus != 1 || $thumbstatus != 1) {$filestatus[$fileloop] = "GDfailed";}
  }
  }
if ($filestatus[$fileloop] == "GDfailed"){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=2";
}
if ($filestatus[$fileloop] == "filetoolarge"){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=3";
}	
if ($filestatus[$fileloop] == "failed"){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=4";
}
if ($filestatus[$fileloop] == "incorrectfile"){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=5";
}	
$fileloop++;
$filename++;
}
/////end of upload

$totalsize = ($_FILES["image1"]["size"] + $_FILES["image2"]["size"] + $_FILES["image3"]["size"] + $_FILES["image4"]["size"] + $_FILES["image5"]["size"]);

if ($redirect == ""){
  $Curl_Session = curl_init();
  curl_setopt ($Curl_Session, CURLOPT_URL, "http://www.my_forum/cgi-bin/yabb/YaBB.pl?action=viewgallery;ref=addalbum2");
  curl_setopt ($Curl_Session, CURLOPT_POST, 1);
  curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "username=$username&viewuser=$viewuser&formsession=$formsession&gallerysecurity=zpqm1000&albumname=$albumname&albumdescription=$albumdescription&albumsecurity=$albumsecurity&image1=$fullfilename[1]&image1title=$image1title&image1description=$image1description&image2=$fullfilename[2]&image2title=$image2title&image2description=$image2description&image3=$fullfilename[3]&image3title=$image3title&image3description=$image3description&image4=$fullfilename[4]&image4title=$image4title&image4description=$image4description&image5=$fullfilename[5]&image5title=$image5title&image5description=$image5description&totalsize=$totalsize&galid=$timestamp");
  curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
  $result = curl_exec   ($Curl_Session);
  curl_close  ($Curl_Session);
if ($result == 1){
  $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;album=$timestamp"; 
}else{
    $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=1";
  }
}
header ( $redirect );








?>

SORTED!

 

After a bit more playing to see when the error occurs it was happening everytime the image width was above the value that the script decides to resize, so I looked at the resizing portion and found I had miss-typed a variable.

 

A simple error!  Amazing how it can stare you in the face!

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.