1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

crop image

Discussion in 'PHP' started by chxxangie, Nov 10, 2008.

  1. #1
    i don know which part is going wrong... sometime the cropped image is not follow the x,y,width n height (cropper). and sometime the cropped image will have the black portion. i found that when the height is greater than width, then the crop result is not correct... i'd check the code many times... can't even know where is going wrong... :(
    anyone help me to see this. i will appreciate ur help...


    [​IMG]

    
     
    <?php
    require("session.php");
     
    $images_path = "myimage/temp/";  //replace this with the path where your images are stored, relative to the directory this script will run from
    $thumbs_path = "myimage/cropped/"; //this is you destination thumbmail folder, you can change it if you want, just be sure the folder actually exists and is writeable by the web server.  In this example, we don't deal with duplicate names, so we just store the cropped verion in a different folder.
     
    // first, we retrieve the form's value, presumably sent through the ost method.
    $photo = $_SESSION['image_filename'];
    $top = isset($_POST['top']) ? $_POST['top'] : false;
    $left = isset($_POST['left']) ? $_POST['left'] : false;
    $width = isset($_POST['width']) ? $_POST['width'] : false;
    $height = isset($_POST['height']) ? $_POST['height'] : false;
     
    //we make sure all the required parameters are present
    if(!($photo && is_numeric($top) && is_numeric($left) && is_numeric($width) && is_numeric($height))){
        die("Incomplete Request");
    }
     
     
    /*
    we are going to suppose that $photo contains the source image file name, which it should if you passed it properly from UvumiCrop in the first place.*/
    $source_file = $images_path.$photo;
     
    //we make sure the source file exist
    if(!file_exists($source_file)) {
        die("file not found.");
    }
     
    //create the target folder if it doesn't exist
    if(!file_exists($thumbs_path)) {
        mkdir($thumbs_path);
    }
     
    //We get the file extension from the file name. It's needed later
    // check image extension
    $extension = pathinfo($photo, PATHINFO_EXTENSION);
     
    //we create a new filename from the original with the "thumb" suffix.
    $thumb = $photo;
     
    //the full target path + file name
    $target_file = $thumbs_path . $thumb;
     
    // we use the the GD library to load the image, using the file extension to choose the right function
    switch(strtolower($extension)) {
        case "jpg":
            $source_image = imagecreatefromjpeg($source_file);
            break;
        case "jpeg":
            $source_image = imagecreatefromjpeg($source_file);
            break;     
        default:
            echo("Error Invalid Image Type");
            die;                   
            break;
    }
     
    /*
    now comes the tricky part
    We have to decide what will be the maximum size of your thumbnails
    for this example we set it to we 160*160, but you may allow non-square images,
    so you have to do some calculation, depending if the crop region is a portrait or lanspace format
    */
     
    $max_width = 400;//$_SESSION['max_svr_res_width'] ; //change this value to set your thumbnail max width
    $max_height = 400;//$_SESSION['max_svr_res_height']; //change this value to set your thumbnail max height
    echo "max width for crop: ".$max_width."<br>";
    echo "max height for crop: ".$max_height."<br>";
     
    if($width > $height) {
        $dest_width = $max_width;
        $dest_height = round($max_width*$height/$width);
    } else {
        $dest_width = round($max_height*$width/$height);
        $dest_height = $max_height;
    }
     
    //we generate a new image object of the size calculated above, using PHP's GD functions
    $dest_image = imagecreatetruecolor($dest_width, $dest_height);
     
     
    /*
    this is where we crop the image,
    -the first parameter is the destinatation image (not a physical file, but a GD image object)
    -second is the source image. Again it's not the physical file but a GD object (which was generated from an image file this time)
    -third and fourth params are the X and Y coordinates to paste the copied region in the destination image. In this case we want both of them to be 0,
    -fifth and sixth are the X and Y coordinates to start cropping in the source image. So they are pretty much the coordinates we got from UvumiCrop.
    -seventh and eighth are the width and height of the destination image, the one calculated right above
    -ninth and tenth are the width and height of the cropping region, directly from UvumiCrop again
     
    By just setting $max_width and $max_height above, you should not have to worry about this
    */
    imagecopyresampled($dest_image, $source_image, 0, 0, $left, $top, $dest_width, $dest_height, max($width, $max_width), max($height, $max_height));
     
    //just as we used $extension to pick the loading function, we'll use it again here to determine which GH function we need for outputting the cropped image
    switch(strtolower($extension)) {
        case "jpg":
            imagejpeg($dest_image, $target_file);
            break;
        case "jpeg":
            imagejpeg($dest_image, $target_file);
            break;
        default:
            echo("Error Invalid Image Type");
            die;
            break;
    }
     
    if($_SESSION['debug'] == 1){
        echo "cropped!";
    }
    ?>
     
    <SCRIPT LANGUAGE="Javascript">
        //alert ('No blank allowed!');
        document.location = "preview.php";
    </SCRIPT>
     
    
    PHP:
     
    chxxangie, Nov 10, 2008 IP