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
https://forums.phpfreaks.com/topic/83508-solved-help-with-function/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.