Jump to content

[SOLVED] help with function


shage

Recommended Posts

function createThumbs( $pathToImages, $pathToThumbs)
{
  $dir = opendir( $pathToImages );
echo "<b>Creating Thumbnails</b> <br />";
  while (false !== ($fname = readdir( $dir ))) {
    $info = pathinfo($pathToImages . $fname);
    if ( strtolower($info['extension']) == 'jpg' )
    {
      echo "Creating thumbnail for <b>{$fname}</b> <br />";

$width = 150;
$height = 150;
$dimensions = getimagesize("{$fname}");
$canvas = imagecreatetruecolor($width,$height);
$piece = imagecreatefromjpeg("{$fname}");
$newwidth = $dimensions[0] / 2;
$newheight = $dimensions[1] / 2;
$cropLeft = ($newwidth/2) - ($width/2);
$cropHeight = ($newheight/2) - ($height/2);
imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight, $width, $height, $newwidth, $newheight);
unset($piece);
if (imagejpeg($canvas,"{$pathToThumbs}{$fname}",90)) 
{
echo 'Image crop successful<br />';
} else {
echo 'Image crop failed<br />';
}
    }
  }
    unset($piece);
  closedir( $dir );
}
createThumbs("imagecreation/","imagecreation/images/thumbnails/");

 

 

when script runs it outputs

 

Creating thumbnail for Vicky07.jpg

 

Warning: getimagesize(Vicky07.jpg) [function.getimagesize]: failed to open stream: No such file or directory

 

file is there, chmod is right, and hell its getting the filename so it must be a real file or directory

Link to comment
Share on other sites

You need to specify the path to the file, not just the file:

<?php
function createThumbs( $pathToImages, $pathToThumbs)
{
  $dir = opendir( $pathToImages );
echo "<b>Creating Thumbnails</b> <br />";
  while (false !== ($fname = readdir( $dir ))) {
    $info = pathinfo($pathToImages . $fname);
    if ( strtolower($info['extension']) == 'jpg' )
    {
      echo "Creating thumbnail for <b>{$fname}</b> <br />";

$width = 150;
$height = 150;
$dimensions = getimagesize($pathToImages . $fname);
$canvas = imagecreatetruecolor($width,$height);
$piece = imagecreatefromjpeg($pathToImages . $fname);
$newwidth = $dimensions[0] / 2;
$newheight = $dimensions[1] / 2;
$cropLeft = ($newwidth/2) - ($width/2);
$cropHeight = ($newheight/2) - ($height/2);
imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight, $width, $height, $newwidth, $newheight);
unset($piece);
if (imagejpeg($canvas, $pathToThumbs . $fname,90)) 
{
echo 'Image crop successful<br />';
} else {
echo 'Image crop failed<br />';
}
    }
  }
    unset($piece);
  closedir( $dir );
}
createThumbs("imagecreation/","imagecreation/images/thumbnails/");
?>

 

Since you're doing this for pictures uploaded from a camera, you might want to check to see if the original images have thumbnails stored with them. Take a look at the exif_thumbnail() function.

 

 

Ken

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.