"Edward Peloke" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> won't the gd library do this?
>
> http://www.php.net/manual/en/function.exif-thumbnail.php
>
> if there is a better function..I am interested in it also as I have to do
> this something this week for a project.
>
> Thanks,
> Eddie

This is what I'm using (of course the GD library is required). Hope the
comments make everything clear. I have only used it with JPEGs, though:


    /**
     * Creates and saves a thumbnail of the supplied image
     *
     * @param string $fileName source file name
     * @param string $sourceDir source directory
     * @param string $targetDir target directory
     * @param string $fileNameExtension file name extension (e.g.:
image.jpeg -> image_thumb.jpg)
     * @param integer $dimension target image
     * @param string $dimensionType ('height', 'width', 'min')
     */
    function createImageThumbnail($fileName, $sourceDir, $targetDir,
$fileNameExtension, $dimension = 60, $dimensionType = 'width')
    {
        // Set file name, file, extension and target file name
        $file            = $sourceDir . $fileName;
        $pointPos        = strrpos($fileName, '.');
        $extOrig         = substr($fileName, $pointPos + 1);
        $ext             = strtolower($extOrig);
        $targetFileName  = substr($fileName, 0, strlen($fileName) -
strlen($ext) - 1) . $fileNameExtension;


        // Create new temp image in memory depending on image type

            // JPEG
            if        ($ext == 'jpg' || $ext == 'jpeg')
                    {
                        $img = imagecreatefromjpeg($file);
                    }

            // GIF
            elseif    ($ext == 'gif')
                    {
                        $img = imagecreatefromgif($file);
                    }

            // PNG
            elseif    ($ext == 'png')
                    {
                        $img = imagecreatefrompng($file);
                    }

            // None of the above, return false
            else    {
                        return false;
                    }

        // Get source dimensions
        $sourceWidth  = imagesx($img);
        $sourceHeight = imagesy($img);

        // Determine smaller side if dimension type is 'min'
        if  ($dimensionType == 'min')
            {
                // if width is smaller than height
                if      ($sourceWidth < $sourceHeight)
                        {
                            $dimensionType = 'width';
                        }
                else    {
                            $dimensionType = 'height';
                        }
            }

        // Calculate dimension
        if      ($dimensionType == 'width')
                {
                    // Calculate target height relative to supplied target
width
                    // (Resize bigger so that dirty right hand pixels will
be outside of new image)
                    $targetWidth  = $dimension;
                    $targetHeight = round(($targetWidth / $sourceWidth) *
$sourceHeight, 0);
                }
        else    {
                    // Calculate target width relative to supplied target
height
                    // (Resize bigger so that dirty right hand pixels will
be outside of new image)
                    $targetHeight  = $dimension;
                    $targetWidth = round(($targetHeight / $sourceHeight) *
$sourceWidth, 0);
                }

        // set oversize
        $overSizePercentage   = 1;
        $overSizePixelsWidth  = round($targetWidth * ($overSizePercentage /
100), 0);
        $overSizePixelsHeight = round($targetHeight * ($overSizePercentage /
100), 0);

        // Create new-sized target image in memory
        // (Height should be smaller so that dirty bottom end pixels will be
outside)
        $new_img = imagecreatetruecolor($targetWidth, $targetHeight);

        if      (!$new_img)
                {
                    // Create new-sized target image in memory
                    // (Height should be smaller so that dirty bottom end
pixels will be outside)
                    $new_img = imagecreate($targetWidth, $targetHeight);
                    imagecopyresized($new_img, $img, 0, 0, 0, 0,
$targetWidth + $overSizePixelsWidth, $targetHeight + $overSizePixelsHeight,
$sourceWidth, $sourceHeight);
                }
        else    {
                    imagecopyresampled($new_img, $img, 0, 0, 0, 0,
$targetWidth + $overSizePixelsWidth, $targetHeight + $overSizePixelsHeight,
$sourceWidth, $sourceHeight);
                }

        // Save target image depending on image type

            // JPEG
            if      ($ext == 'jpg' || $ext == 'jpeg')
                    {
                        imagejpeg($new_img, $targetDir . $targetFileName .
'.' . $extOrig);
                    }

            // GIF
            elseif  ($ext == 'gif')
                    {
                        imagegif($new_img, $targetDir . $targetFileName .
'.' . $extOrig);
                    }

            // PNG
            elseif  ($ext == 'png')
                    {
                        imagepng($new_img, $targetDir . $targetFileName .
'.' . $extOrig);
                    }

        // Clear memory
        imagedestroy($new_img);
        imagedestroy($img);
    }


Regards, Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to