Jump to content


Photo

saving cropped images to database or directory


  • Please log in to reply
1 reply to this topic

#1 dsaba

dsaba
  • Members
  • PipPipPip
  • Advanced Member
  • 724 posts

Posted 27 February 2006 - 04:34 PM

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

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 27 February 2006 - 05:57 PM

With a little adjusting this should do what you want. Simply replace the resize part with your crop 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); 
}

?>

hope it helps.

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users