November 3, 201015 yr Hi, I've got the following script which grabs an image uploaded through Flash, stores on the server and generates a thumbnail. I'm using GD imagecopyresize to generate the thumbnails which is working fine except that the resized result always comes out black? Any ideas why this could be? <?php // Set local PHP vars from the POST vars sent from flash $todayDate = $_POST['todayDate']; $Name = $_POST['Name']; $Email = $_POST['Email']; $filename = $_FILES['Filedata']['name']; $filetmpname = $_FILES['Filedata']['tmp_name']; $fileType = $_FILES["Filedata"]["type"]; $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000); // Place file on server, into uploadimages move_uploaded_file($_FILES['Filedata']['tmp_name'], "uploadimages/".$filename); // Logfile $myFile = "logFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "\n\ntodayDate: $todayDate \n Name: $Name \n Email: $Email \n ssid: $ssid \n FileName: $filename \n TmpName: $filetmpname \n Type: $fileType \n Size: $fileSizeMB MegaBytes"; fwrite($fh, $stringData); fclose($fh); // End log file edit // load image and get image size $img = imagecreatefromjpeg( "uploadimages/.$filename" ); $width = imagesx( $img ); $height = imagesy( $img ); // set thumbnail size $new_width = 100; $new_height = 100; // create a new tempopary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a new file in uploadthumbs imagejpeg( $tmp_img, "uploadthumbs/$filename" ); ?>
November 3, 201015 yr Try changing this line: $img = imagecreatefromjpeg( "uploadimages/.$filename" ); to: $img = imagecreatefromjpeg( "uploadimages/$filename" );
November 3, 201015 yr GD does not deal with PNG/GIF images with transparent background very well by default, at least it is not as easy to use as Imagmagick. and yes GD will fill the background with black. I used the script from http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/ to resize images using GD as not all hosting servers got imagmagick That script can deal with transparent background properly Try changing this line: $img = imagecreatefromjpeg( "uploadimages/.$filename" ); to: $img = imagecreatefromjpeg( "uploadimages/$filename" );
November 4, 201015 yr Try changing this line: $img = imagecreatefromjpeg( "uploadimages/.$filename" ); to: $img = imagecreatefromjpeg( "uploadimages/$filename" ); Didn't see that! Makes sense, imagecreatetruecolor() makes a black image and then you'd be resizing null into it.
November 4, 201015 yr Author That's great guys, yeah it was the period proceeding the $filename var. Coming out full colour now. Thanks!
Create an account or sign in to comment