Jump to content

Thumbnail code creates completly black thumbnails!


SyncViews

Recommended Posts

Why does this create a black image?

<?php
$file_full   = './images/mytrain.jpg';
$file_thumb  = './images/thumbnails/mytrain.jpg';
$file_thumb2 = './images/thumbnails/mytrain.png';

$image_full  = imagecreatefromjpeg ($file_full);
$image_thumb = imagecreatetruecolor(400, 300);

$image_image_x = imagesx($image_full);
$image_image_y = imagesy($image_full);

imagecopyresampled ($image_thumb, $image_full, 0, 0, 0, 0, 400, 300, $file_image_x, $file_image_y);

imagejpeg($image_thumb, $file_thumb, 80);
imagepng($image_thumb, $file_thumb2, 5);
imagedestroy ($image_full);
imagedestroy ($image_thumb);
?>

Link to comment
Share on other sites

no i'm not but it was playing up without the ./ as well...

 

ive aslso got this on the page so I know the paths are correct...

 

<img src="<?=$file_full?>">

<img src="<?=$file_thumb?>">

<img src="<?=$file_thumb2?>">

 

 

EDIT: also if the path is wrong i get this followed by another 5 or 6 errors

Warning: imagecreatefromjpeg(/images/mytrain.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory...

Link to comment
Share on other sites

Still havn't solved this :(

 

<?php
$file_full   = 'images/mytrain.jpg';
$file_full2  = 'images/mytrain2.png';
$file_thumb  = 'mytrain.jpg';
$file_thumb2 = 'mytrain.png';


$image_full  = imagecreatefromjpeg ($file_full);
$image_thumb = imagecreatetruecolor(400, 300);

$image_image_x = imagesx($image_full);
$image_image_y = imagesy($image_full);
imagecopyresampled ($image_thumb, $image_full, 0, 0, 0, 0, 400, 300, $file_image_x, $file_image_y);

imagejpeg($image_thumb, $file_thumb);
imagepng ($image_thumb, $file_thumb2);
imagepng ($image_full, $file_full2);
imagedestroy ($image_full);
imagedestroy ($image_thumb);
?>
<img src="<?=$file_full?>">
<img src="<?=$file_thumb?>">
<img src="<?=$file_thumb2?>">
<img src="<?=$file_full2?>">

 

The tumbnails just show up black while th other images work...I presume it is that the copy/resise doesn't work but I can't see anytrhing wrong with it :(

 

Also is there anyway to get a useful error with thse functions? eg eith  the mysql_ functions you can use mysql_error() to get a pretty usful error message if something fails?

Link to comment
Share on other sites

i found this function, which iterates over a file and creates thumbnails of all the jpeg images in it

<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
SureRemoveDir($pathToThumbs, false);
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
           // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary 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 file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }	
  }
  // close the directory
  closedir( $dir );
}
?>
//called like this
createThumbs( "images/general/", "images/gen_th/", 100 );

Link to comment
Share on other sites

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.