Jump to content

saving cropped images to database or directory


dsaba

Recommended Posts

below is the script i'm having trouble with, all it does for now is crop just one image and then displays it, I would like to manipulate it so I can have it crop hundreds of images by linking to a directory or to a database where I store the urls to the original images, I would also like to have it save these hundreds of images that it crops, maybe in a directory or in a database, don't know where to start really...........
-----------------------------------------------------------------------------------------------------
<?php
//on top left corner (483, 216)
//on top right corner (657, 216)
//on bottom left corner (483, 346)
//on bottom right corner (657, 346)
// 172 width, by 131 high dimensions

$imgfile = "http://localhost/pics/pic1.JPG";
$cropStartX = 483;
$cropStartY = 216;
$cropW = 172;
$cropH = 131;

// Create two images
$origimg = imagecreatefromjpeg($imgfile);
$cropimg = imagecreatetruecolor($cropW,$cropH);

// Get the original size
list($width, $height) = getimagesize($imgfile);

// Crop
imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

// TODO: write code to save new image
// or, just display it like this:
header("Content-type: image/jpeg");
imagejpeg($cropimg);

// destroy the images
imagedestroy($cropimg);
imagedestroy($origimg);

?>
----------------------------------------------------------------------------------------------------
it works and it crops the image, but I would like to have it do multiple images at the same time, and also have it save the new cropped image for me
any suggestions? maybe let it access a database where I supply the url of the images? and have it save the images there, or just create a new directory for the images?, I need to crop hundreds at a time
thanks people
Link to comment
Share on other sites

With a little adjusting this should do what you want. Simply replace the resize part with your crop code.

[code]<?PHP

###########################################
#
# script to take all the images in a folder and resample them.  it will loop through all the images
# in a folder and check to see if width is > 800 and/or height > 600 . If the image meets
# either of those criteria, the script will resample the image to fall within the guidelines
#
############################################

// set path to image folder

$image_folder = "pics";

if ($handle = opendir($image_folder))
{
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != ".." )
        {
            $image_name = $image_folder . '/' . $file;
            $image_type = strstr($image_name, '.');

            switch($image_type) {
                case '.jpg':
                    $src = imagecreatefromjpeg($image_name);
                    break;
                case '.png':
                    $src = imagecreatefrompng($image_name);
                    $totlen = strlen($image_name) - 3;
                    $newname = substr($image_name, 0, $totlen) . "jpg";
                    unlink($image_name);
                    $image_name = $newname;
                    break;
                case '.gif':
                    $src = imagecreatefromgif($image_name);
                    $totlen = strlen($image_name) - 3;
                    $newname = substr($image_name, 0, $totlen) . "jpg";
                    unlink($image_name);
                    $image_name = $newname;

                    break;
                default:
                    echo "Error Invalid Image Type: " . $image_name . " x x x " . $image_type;
                    die;
                    break;
            }

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

            // check that width is 800 or less
            if ($width > 800)
            {
                $ratio_w = 800/$width;
            } else {
                $ratio_w = 1;
            }

            // check if height is 600 or less
            if ($height > 600)
            {
                $ratio_h = 600/$height;
            } else {
                $ratio_h = 1;
            }

            // determine which ratio creates the smaller image
            if ($ratio_w <= $ratio_h)
            {
                $aspect_ratio = $ratio_w;
            } else {
                $aspect_ratio = $ratio_h;
            }

            // new resizing code
            $new_w = abs($width * $aspect_ratio);
            $new_h = abs($height * $aspect_ratio);
            $size = $new_h;

            $aspect_ratio = $height/$width;

            //start resizing
            if ($height <= $size)
            {
                $new_w = $width;
                $new_h = $height;
            } else {
                $new_h = $size;
                $new_w = abs($new_h / $aspect_ratio);
            }


            $img = imagecreatetruecolor ($new_w,$new_h);

            //save image
            imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);

            imagejpeg($img, $image_name, 90);
            imagedestroy($img);

        }
    }
    closedir($handle);
}

?>[/code]

hope it helps.

Lite...
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.