Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

GD resize coming out black?

Featured Replies

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" );

?>

Try changing this line:

$img = imagecreatefromjpeg( "uploadimages/.$filename" );

to:

$img = imagecreatefromjpeg( "uploadimages/$filename" );

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" );

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.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.