Jump to content

Recommended Posts

I am trying to use PHP GD module to create thumbnail images from this example http://icant.co.uk/articles/phpthumbnails/

It is generating thumbnails but they are black. I have read alot about this issue and tried them all but still won't work. I changed the function names to lowercase. I used ImageCreateTrueColor()  instead of imagecreatefromjpeg() . I have tired different path settings. And it also gives me errors such as

Warning: imagesx(): supplied argument is not a valid Image resource in .....

 

Warning: imagesy(): supplied argument is not a valid Image resource in ....

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in ....

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in ....

 

 

Below is the code.

 

<?php 
$imagefolder="images";
$thumbsfolder="thumbnails";
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
	createthumb($p,"tnh_".$p,100,100);
}
}

/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
	if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}

/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name		Original filename
$filename	Filename of the resized image
$new_w		width of resized image
$new_h		height of resized image
*/	
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=ImageCreateTrueColor($name);}
if (preg_match("/png/",$system[1])){$src_img=ImageCreateTrueColor($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) 
{
	$thumb_w=$old_x*($new_w/$old_y);
	$thumb_h=$new_h;
}
if ($old_x == $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}
$dst_img=ImageCreateTruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
if (preg_match("/png/",$system[1]))
{
	imagepng($dst_img,$filename); 
} else {
	imagejpeg($dst_img,$filename); 
}
imagedestroy($dst_img); 
imagedestroy($src_img); 
}

/*
        Function directory($directory,$filters)
        reads the content of $directory, takes the files that apply to $filter 
	and returns an array of the filenames.
        You can specify which files to read, for example
        $files = directory(".","jpg,gif");
                gets all jpg and gif files in this directory.
        $files = directory(".","all");
                gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
	$filters=explode(",",$filters);
	while (($file = readdir($handle))!==false)
	{
		for ($f=0;$f<sizeof($filters);$f++):
			$system=explode(".",$file);
			if ($system[1] == $filters[$f]){$files[] = $file;}
		endfor;
	}
}
closedir($handle);
return $files;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/70957-solved-php-gd-creating-thumbnails/
Share on other sites

if nothing else, you're calling ImageCreateTrueColor incorrectly. It takes width and height as parameters:

 

resource imagecreatetruecolor ( int width, int height )

 

probably should be more like:

 

if (preg_match("/jpg|jpeg/",$system[1])) {
     $src_img= imagecreatefromjpeg($name);
}

if (preg_match("/png/",$system[1])) {
    $src_img= imagecreatefrompng($name);
}

 

... make sure $name is a fully-qualified path name.

function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])) {
     $src_img= imagecreatefromjpeg($name);
}

if (preg_match("/png/",$system[1])) {
    $src_img= imagecreatefrompng($name);
}

 

What do you mean make sure $ name is a qualified path? What should be after explode"." 

I fixed the problem. I was entering in a file path like "images" but I left it like below and it worked. But now I want to see if i can change the path. The file resides in the same folder as the image files. If I moved it out of the current folder, would I leave the dot like ./images  ?

 

$imagefolder='.';
$thumbsfolder='.';

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.