trying to use the GD library

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

trying to use the GD library

Post by chopWood »

1. I am trying to use the GD library in a script (located at the bottom of this page) that resizes an iimage but come up with the following errors:
----------
[30-Apr-2010 14:03:18] PHP Notice: Undefined index: upfile in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 44
[30-Apr-2010 14:03:18] PHP Notice: Undefined index: upfile in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 45
[30-Apr-2010 14:03:34] PHP Warning: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open 'thumbs/daveonPier.jpg' for writing: No such file or directory in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 40
-----------


2. Within my pphpInfo() I get the following that indicates the library is installed:
-------------
Command './configure' '--with-mysql=/Applications/MAMP/Library' '--with-apxs2=/Applications/MAMP/Library/bin/apxs' '--with-gd' '--enable-bcmath' '--enable-ftp' '--enable-gd-native-ttf'

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
--------------

3. The following script returns 'mkthumb not present' though the mkthumb() function is supposed to be part of the library

Code: Select all

<?php
if (function_exists(mkthumb)){
print 'mkthumb present';
}else {
print 'mkthumb not present';
print' ';
}
?>




4. I am using the following:
------------
Apache/2.0.63 (Unix)
PHP/5.2.11 DAV/2
MySQL client version: 5.1.37
-------------

Can someone start me off on how to fix this?
Appreciated,
chopWood

Code: Select all

<?php

function mkthumb($filename, $thumbname) {
    /* generate an image thumbnail */
    $thumb_width = 125; 
    $thumb_height = 125; 

    if (preg_match('/\.gif$/i', $filename)) {
	$src_image = imagecreatefromgif($filename); 
    } else if (preg_match('/\.png$/i', $filename)) {
	$src_image = imagecreatefrompng($filename); 
    } else {
	/* assume jpeg by default */
	$src_image = imagecreatefromjpeg($filename);
    }

    $width = imagesx($src_image);
    $height = imagesy($src_image);

    if (($height > $thumb_height) || ($width > $thumb_width)) {
	/* create a thumbnail */
	if ($width > $height) {
	    $ratio = $thumb_width / $width; 
	} else {
	    $ratio = $thumb_height / $height;
	}

	$new_width = round($width * $ratio);
	$new_height = round($height * $ratio); 

	$dest_image = ImageCreateTrueColor($new_width, $new_height);
	imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0,
			   $new_width, $new_height, $width, $height); 
	imagedestroy($src_image); 
    } else {
	/* image is already small enough; just output */
	$dest_image = $src_image;
    }

    imagejpeg($dest_image, $thumbname); 
    imagedestroy($dest_image); 
} 

$upfile = $_FILES["upfile"]["tmp_name"];
$fn = $_FILES["upfile"]["name"];
$thumb_filename = "thumbs/$fn";

if ($upfile) {
    mkthumb($upfile, $thumb_filename);
    print "<img src=\"$thumb_filename\" />";
} else { ?>
    <form action="thumb.php" enctype="multipart/form-data" method="post">
    Upload an image:<br />
    <input name="upfile" type="file" /><br />
    <input name="Submit" type="submit" value="Upload Image" />
    <?
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: trying to use the DG library

Post by requinix »

chopWood wrote:1. I am trying to use the GD library in a script (located at the bottom of this page) that resizes an iimage but come up with the following errors:
----------
[30-Apr-2010 14:03:18] PHP Notice: Undefined index: upfile in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 44
[30-Apr-2010 14:03:18] PHP Notice: Undefined index: upfile in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 45
[30-Apr-2010 14:03:34] PHP Warning: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open 'thumbs/daveonPier.jpg' for writing: No such file or directory in /PhpSql/quickStart_Practice/wcphp/thumb.php on line 40
1. You're trying to look at $_FILES when nothing has been uploaded yet. Can't do that.
2. The "thumbs" directory doesn't exist.
chopWood wrote:3. The following script returns 'mkthumb not present' though the mkthumb() function is supposed to be part of the library
No, it isn't. It's a function you defined in your script. The code to test it will only work after the file has been included (or if placed in that file).
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: trying to use the DG library

Post by chopWood »

thanks very much tasairis, it worked after I created the directory. Great!
Post Reply