GD Image creation Help

    Date: 07/15/05 (PHP Community)    Keywords: asp

    Hey all.. I've got a bit of a problem here. I'm using the simplevieweradmin to manage a simpleviewer flash gallery on a site I'm working on. By default, the admin allows you to upload a file and create a square thumbnail. My client requires that we keep the aspect ratio the same, so, I'm trying to modify the simplevieweradmin thumb creation function to 1st create a blank square thumb, then insert an uncropped thumbnail with the max height or width to be the same as the square height/width of the thumb. I've found some tut's on how to do this, but am having alot of trouble putting it together.

    Code is behind the cut. Any help would be greatly appreciated.



    We assume the thumbnail should be 45 pixels, either wide or high.
    We load the original image, and check its dimensions.
    If the picture is higher than wide, we set the height of the thumb to 45 pixels.
    The width of the thumbnail is the original width multiplied with 45 pixels divided by its height.
    Thumbnail height = original width * (45 / original height)
    This way we preserve the original aspect ratio.
    If the original picture is wider than high, we do the same to the height of the thumbnail.
    If they are the same, we simply create a 45x45 pixels image.


    function createImage($file, $format, $mode, $fileDest)
    {
    // Get information about the installed GD.
    $gdInfo = checkGD();
    $gdVersion = $gdInfo['version'];

    if ($gdInfo == false) {
    return false;
    }

    // Ensure the given format is supported.
    if ($gdInfo['formats'][$format] != 1) {
    return false;
    }

    // Get the image dimensions.
    $dimensions = @getimagesize($file);
    $width = $dimensions[0];
    $height = $dimensions[1];

    // images mode.
    if ($mode == 'images') {
    $deltaX = 0;
    $deltaY = 0;
    $portionX = $outputX = $width;
    $portionY = $outputY = $height;
    $quality = 100;
    }

    // thumbs mode.
    if ($mode == 'thumbs') {
    $outputX = 45;
    $outputY = 45;
    $quality = 85;

    // The image is of vertical shape.
    if ($height > $width) {
    $deltaX = $width * ($outputY / $height);
    $deltaY = 0;
    $portionX = $width;
    $portionY = $height;

    // The image is of horizontal shape.
    } else if ($width > $height) {
    $deltaX = 0;
    $deltaY = $height * ($outputX / $width);
    $portionX = $width;
    $portionY = $height;

    // The image is of squared shape.
    } else {
    $deltaX = 0;
    $deltaY = 0;
    $portionX = $width;
    $portionY = $height;
    }
    }

    // Get the source image in gif format.
    if ($format == 'gif') {
    $imageSrc = @imagecreatefromgif($file);
    }

    // Get the source image in jpg format.
    if ($format == 'jpg') {
    $imageSrc = @imagecreatefromjpeg($file);
    }

    // Get the source image in png format.
    if ($format == 'png') {
    $imageSrc = @imagecreatefrompng($file);
    }

    // The thumbnail creation with GD1.x functions does the job.
    if ($gdVersion == 1) {

    // Create an empty thumbnail image.
    $imageDest = @imagecreate($outputX, $outputY);

    // Try to create the thumbnail from the source image.
    if (@imagecopyresized($imageDest, $imageSrc, 0, 0, 0, 0, $outputX, $outputY, $width, $height)) {

    // save the thumbnail image into a file.
    @imagejpeg($imageDest, $fileDest, $quality);

    // Delete both image resources.
    @imagedestroy($imageSrc);
    @imagedestroy($imageDest);

    return true;

    }

    }

    // The recommended approach is the usage of the GD2.x functions.
    if ($gdVersion == 2) {

    // Create an empty thumbnail image.
    $imageDest = @imagecreatetruecolor($outputX, $outputY);

    // Try to create the thumbnail from the source image.
    if (@imagecopyresampled($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $outputX, $outputY, $portionX, $portionY)) {

    // save the thumbnail image into a file.
    @imagejpeg($imageDest, $fileDest, $quality);

    // Delete both image resources.
    @imagedestroy($imageSrc);
    @imagedestroy($imageDest);

    return true;

    }

    }

    return false;
    }

    Source: http://www.livejournal.com/community/php/319300.html

« Where am I? || PHP5 on OSX Tiger »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home