SyncViews Posted December 13, 2007 Share Posted December 13, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted December 13, 2007 Share Posted December 13, 2007 $file_full = './images/mytrain.jpg'; Are you trying to access the folders below the current directory? If so it should be: $file_full = '../images/mytrain.jpg'; Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 13, 2007 Author Share Posted December 13, 2007 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... Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 13, 2007 Author Share Posted December 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 14, 2007 Author Share Posted December 14, 2007 Does nobody have ansew for this? There must be some basic reason why my code doesn't work... Quote Link to comment Share on other sites More sharing options...
paul2463 Posted December 14, 2007 Share Posted December 14, 2007 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 ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.