GUI Code
  Home arrow GUI Code arrow Crop Canvas
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
Case Studies 
iPad Development 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
GUI CODE

Crop Canvas
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-08-01

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    This class allows you to crop an image in a variety of ways. You can crop in an absolute or relative way (to a certain size or by a certain size), both as a pixel number or a percentage. You can also save or display the cropped image. The cropping can be done in 9 different positions: top left, top, top right, left, centre, right, bottom left, bottom, or bottom right. Or you can automatically crop based on a threshold limit. The original image can be loaded from the file system or from a string (for example, data returned from a database.)

    The script attempts to auto-detect the GD version, so you should be able to use GD 1.6 and on up.

    By : amnuts


    <?php

    //
    // class.cropcanvas.php
    // version 1.2.0, 26th November, 2003
    //
    // Description
    //
    // This is a class allows you to crop an image in a variety of ways.
    // You can crop in an absolute or relative way (to a certain size or
    // by a certain size), both as a pixel number or a percentage. You
    // can also save or display the cropped image. The cropping can be
    // done in 9 different positions: top left, top, top right, left,
    // centre, right, bottom left, bottom, or bottom right. Or you can
    // crop automatically based on a threshold limit. The original
    // image can be loaded from the file system or from a string (for
    // example, data returned from a database.)
    //
    // Author
    //
    // Andrew Collington, 2003
    // php@amnuts.com, http://php.amnuts.com/
    //
    //
    // Feedback
    //
    // There is message board at the following address:
    //
    // http://php.amnuts.com/forums/index.php
    //
    // Please use that to post up any comments, questions, bug reports, etc. You
    // can also use the board to show off your use of the script.
    //
    // Support
    //
    // If you like this script, or any of my others, then please take a moment
    // to consider giving a donation. This will encourage me to make updates and
    // create new scripts which I would make available to you. If you would like
    // to donate anything, then there is a link from my website to PayPal.
    //
    // Example of use
    //
    // require 'class.cropcanvas.php';
    // $cc = new canvasCrop();
    //
    // $cc->loadImage('original1.png');
    // $cc->cropBySize(100, 100, ccBOTTOMRIGHT);
    // $cc->saveImage('final1.png');
    //
    // $cc->flushImages();
    //
    // $cc->loadImage('original2.png');
    // $cc->cropByPercent(15, 50, ccCENTER);
    // $cc->saveImage('final2.jpg', 90);
    //
    // $cc->flushImages();
    //
    // $cc->loadImage('original3.png');
    // $cc->cropToDimensions(67, 37, 420, 255);
    // $cc->showImage('png');
    //


    define("ccTOPLEFT", 0);
    define("ccTOP", 1);
    define("ccTOPRIGHT", 2);
    define("ccLEFT", 3);
    define("ccCENTRE", 4);
    define("ccCENTER", 4);
    define("ccRIGHT", 5);
    define("ccBOTTOMLEFT", 6);
    define("ccBOTTOM", 7);
    define("ccBOTTOMRIGHT", 8);


    class canvasCrop
    {
    var $_imgOrig;
    var $_imgFinal;
    var $_showDebug;
    var $_gdVersion;


    /**
    * @return canvasCrop
    * @param bool $debug
    * @desc Class initializer
    */
    function canvasCrop($debug = false)
    {
    $this->_showDebug = ($debug ? true : false);
    $this->_gdVersion = (function_exists('imagecreatetruecolor')) ? 2 : 1;
    }


    /**
    * @return bool
    * @param string $filename
    * @desc Load an image from the file system - method based on file extension
    */
    function loadImage($filename)
    {
    if (!@file_exists($filename))
    {
    $this->_debug('loadImage', "The supplied file name '$filename' does not point to a readable file.");
    return false;
    }

    $ext = strtolower($this->_getExtension($filename));
    $func = "imagecreatefrom$ext";

    if (!@function_exists($func))
    {
    $this->_debug('loadImage', "That file cannot be loaded with the function '$func'.");
    return false;
    }

    $this->_imgOrig = @$func($filename);

    if ($this->_imgOrig == null)
    {
    $this->_debug('loadImage', 'The image could not be loaded.');
    return false;
    }

    return true;
    }


    /**
    * @return bool
    * @param string $string
    * @desc Load an image from a string (eg. from a database table)
    */
    function loadImageFromString($string)
    {
    $this->_imgOrig = @ImageCreateFromString($string);
    if (!$this->_imgOrig)
    {
    $this->_debug('loadImageFromString', 'The image could not be loaded.');
    return false;
    }
    return true;
    }


    /**
    * @return bool
    * @param string $filename
    * @param int $quality
    * @desc Save the cropped image
    */
    function saveImage($filename, $quality = 100)
    {
    if ($this->_imgFinal == null)
    {
    $this->_debug('saveImage', 'There is no processed image to save.');
    return false;
    }

    $ext = strtolower($this->_getExtension($filename));
    $func = "image$ext";

    if (!@function_exists($func))
    {
    $this->_debug('saveImage', "That file cannot be saved with the function '$func'.");
    return false;
    }

    $saved = false;
    if ($ext == 'png') $saved = $func($this->_imgFinal, $filename);
    if ($ext == 'jpeg') $saved = $func($this->_imgFinal, $filename, $quality);
    if ($saved == false)
    {
    $this->_debug('saveImage', "Could not save the output file '$filename' as a $ext.");
    return false;
    }

    return true;
    }


    /**
    * @return bool
    * @param string $type
    * @param int $quality
    * @desc Shows the cropped image without any saving
    */
    function showImage($type = 'png', $quality = 100)
    {
    if ($this->_imgFinal == null)
    {
    $this->_debug('showImage', 'There is no processed image to show.');
    return false;
    }
    if ($type == 'png')
    {
    echo @ImagePNG($this->_imgFinal);
    return true;
    }
    else if ($type == 'jpg' || $type == 'jpeg')
    {
    echo @ImageJPEG($this->_imgFinal, '', $quality);
    return true;
    }
    else
    {
    $this->_debug('showImage', "Could not show the output file as a $type.");
    return false;
    }
    }


    /**
    * @return int
    * @param int $x
    * @param int $y
    * @param int $position
    * @desc Determines the dimensions to crop to if using the 'crop by size' method
    */
    function cropBySize($x, $y, $position = ccCENTRE)
    {
    if ($x == 0)
    {
    $nx = @ImageSX($this->_imgOrig);
    }
    else
    {
    $nx = @ImageSX($this->_imgOrig) - $x;
    }
    if ($y == 0)
    {
    $ny = @ImageSY($this->_imgOrig);
    }
    else
    {
    $ny = @ImageSY($this->_imgOrig) - $y;
    }
    return ($this->_cropSize(-1, -1, $nx, $ny, $position, 'cropBySize'));
    }


    /**
    * @return int
    * @param int $x
    * @param int $y
    * @param int $position
    * @desc Determines the dimensions to crop to if using the 'crop to size' method
    */
    function cropToSize($x, $y, $position = ccCENTRE)
    {
    if ($x == 0) $x = 1;
    if ($y == 0) $y = 1;
    return ($this->_cropSize(-1, -1, $x, $y, $position, 'cropToSize'));
    }


    /**
    * @return int
    * @param int $sx
    * @param int $sy
    * @param int $ex
    * @param int $ey
    * @desc Determines the dimensions to crop to if using the 'crop to dimensions' method
    */
    function cropToDimensions($sx, $sy, $ex, $ey)
    {
    $nx = abs($ex - $sx);
    $ny = abs($ey - $sy);
    return ($this->_cropSize($sx, $sy, $nx, $ny, $position, 'cropToDimensions'));
    }


    /**
    * @return int
    * @param int $percentx
    * @param int $percenty
    * @param int $position
    * @desc Determines the dimensions to crop to if using the 'crop by percentage' method
    */
    function cropByPercent($percentx, $percenty, $position = ccCENTRE)
    {
    if ($percentx == 0)
    {
    $nx = @ImageSX($this->_imgOrig);
    }
    else
    {
    $nx = @ImageSX($this->_imgOrig) - (($percentx / 100) * @ImageSX($this->_imgOrig));
    }
    if ($percenty == 0)
    {
    $ny = @ImageSY($this->_imgOrig);
    }
    else
    {
    $ny = @ImageSY($this->_imgOrig) - (($percenty / 100) * @ImageSY($this->_imgOrig));
    }
    return ($this->_cropSize(-1, -1, $nx, $ny, $position, 'cropByPercent'));
    }


    /**
    * @return int
    * @param int $percentx
    * @param int $percenty
    * @param int $position
    * @desc Determines the dimensions to crop to if using the 'crop to percentage' method
    */
    function cropToPercent($percentx, $percenty, $position = ccCENTRE)
    {
    if ($percentx == 0)
    {
    $nx = @ImageSX($this->_imgOrig);
    }
    else
    {
    $nx = ($percentx / 100) * @ImageSX($this->_imgOrig);
    }
    if ($percenty == 0)
    {
    $ny = @ImageSY($this->_imgOrig);
    }
    else
    {
    $ny = ($percenty / 100) * @ImageSY($this->_imgOrig);
    }
    return ($this->_cropSize(-1, -1, $nx, $ny, $position, 'cropByPercent'));
    }


    /**
    * @return bool
    * @param int $threshold
    * @desc Determines the dimensions to crop to if using the 'automatic crop by threshold' method
    */
    function cropByAuto($threshold = 254)
    {
    if ($threshold < 0) $threshold = 0;
    if ($threshold > 255) $threshold = 255;

    $sizex = @ImageSX($this->_imgOrig);
    $sizey = @ImageSY($this->_imgOrig);

    $sx = $sy = $ex = $ey = -1;
    for ($y = 0; $y < $sizey; $y++)
    {
    for ($x = 0; $x < $sizex; $x++)
    {
    if ($threshold >= $this->_getThresholdValue($this->_imgOrig, $x, $y))
    {
    if ($sy == -1) $sy = $y;
    else $ey = $y;

    if ($sx == -1) $sx = $x;
    else
    {
    if ($x < $sx) $sx = $x;
    else if ($x > $ex) $ex = $x;
    }
    }
    }
    }
    $nx = abs($ex - $sx);
    $ny = abs($ey - $sy);
    return ($this->_cropSize($sx, $sy, $nx, $ny, ccTOPLEFT, 'cropByAuto'));
    }


    /**
    * @return void
    * @desc Destroy the resources used by the images
    */
    function flushImages()
    {
    @ImageDestroy($this->_imgOrig);
    @ImageDestroy($this->_imgFinal);
    $this->_imgOrig = $this->_imgFinal = null;
    }


    /**
    * @return bool
    * @param int $ox Original image width
    * @param int $oy Original image height
    * @param int $nx New width
    * @param int $ny New height
    * @param int $position Where to place the crop
    * @param string $function Name of the calling function
    * @desc Creates the cropped image based on passed parameters
    */
    function _cropSize($ox, $oy, $nx, $ny, $position, $function)
    {
    if ($this->_imgOrig == null)
    {
    $this->_debug($function, 'The original image has not been loaded.');
    return false;
    }
    if (($nx <= 0) || ($ny <= 0))
    {
    $this->_debug($function, 'The image could not be cropped because the size given is not valid.');
    return false;
    }
    if (($nx > @ImageSX($this->_imgOrig)) || ($ny > @ImageSY($this->_imgOrig)))
    {
    $this->_debug($function, 'The image could not be cropped because the size given is larger than the original image.');
    return false;
    }
    if ($ox == -1 || $oy == -1)
    {
    list($ox, $oy) = $this->_getCopyPosition($nx, $ny, $position);
    }
    if ($this->_gdVersion == 2)
    {
    $this->_imgFinal = @ImageCreateTrueColor($nx, $ny);
    @ImageCopyResampled($this->_imgFinal, $this->_imgOrig, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
    }
    else
    {
    $this->_imgFinal = @ImageCreate($nx, $ny);
    @ImageCopyResized($this->_imgFinal, $this->_imgOrig, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
    }
    return true;
    }


    /**
    * @return array
    * @param int $nx
    * @param int $ny
    * @param int $position
    * @desc Determines dimensions of the crop
    */
    function _getCopyPosition($nx, $ny, $position)
    {
    $ox = @ImageSX($this->_imgOrig);
    $oy = @ImageSY($this->_imgOrig);

    switch($position)
    {
    case ccTOPLEFT:
    return array(0, 0);
    case ccTOP:
    return array(ceil(($ox - $nx) / 2), 0);
    case ccTOPRIGHT:
    return array(($ox - $nx), 0);
    case ccLEFT:
    return array(0, ceil(($oy - $ny) / 2));
    case ccCENTRE:
    return array(ceil(($ox - $nx) / 2), ceil(($oy - $ny) / 2));
    case ccRIGHT:
    return array(($ox - $nx), ceil(($oy - $ny) / 2));
    case ccBOTTOMLEFT:
    return array(0, ($oy - $ny));
    case ccBOTTOM:
    return array(ceil(($ox - $nx) / 2), ($oy - $ny));
    case ccBOTTOMRIGHT:
    return array(($ox - $nx), ($oy - $ny));
    }
    }


    /**
    * @return float
    * @param resource $im
    * @param int $x
    * @param int $y
    * @desc Determines the intensity value of a pixel at the passed co-ordinates
    */
    function _getThresholdValue($im, $x, $y)
    {
    $rgb = ImageColorAt($im, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    $intensity = ($r + $g + $b) / 3;
    return $intensity;
    }


    /**
    * @return string
    * @param string $filename
    * @desc Get the extension of a file name
    */
    function _getExtension($filename)
    {
    $ext = @strtolower(@substr($filename, (@strrpos($filename, ".") ? @strrpos($filename, ".") + 1 : @strlen($filename)), @strlen($filename)));
    return ($ext == 'jpg') ? 'jpeg' : $ext;
    }


    /**
    * @return void
    * @param string $function
    * @param string $string
    * @desc Shows debugging information
    */
    function _debug($function, $string)
    {
    if ($this->_showDebug)
    {
    echo "<p><strong style=\"color:#FF0000\">Error in function $function:</strong> $string</p>\n";
    }
    }


    /**
    * @return array
    * @desc Try to ascertain what the version of GD being used is, based on phpinfo output
    */
    function _getGDVersion()
    {
    static $version = array();

    if (empty($version))
    {
    ob_start();
    phpinfo();
    $buffer = ob_get_contents();
    ob_end_clean();
    if (preg_match("|<B>GD Version</B></td><TD ALIGN=\"left\">([^<]*)</td>|i", $buffer, $matches))
    {
    $version = explode('.', $matches[1]);
    }
    else if (preg_match("|GD Version </td><td class=\"v\">bundled \(([^ ]*)|i", $buffer, $matches))
    {
    $version = explode('.', $matches[1]);
    }
    else if (preg_match("|GD Version </td><td class=\"v\">([^ ]*)|i", $buffer, $matches))
    {
    $version = explode('.', $matches[1]);
    }
    }
    return $version;
    }

    }

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More GUI Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Software Analyzer V7.0

    Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z: Architecture

    Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Whitepaper: Achieving consistency between business process models and operational guides

    Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    GUI CODE ARTICLES

    - PHP Image Manipulation Class 1.0
    - Simple PHP-CAPTCHA,
    - dPhoto
    - nice looking dir index'r
    - Colorpicker
    - Exposure Gallery build 1226
    - Dynamic "AQUA" Buttons
    - AdminTool|1.0 for Exposure Gallery
    - Exposure Gallery v1.2
    - Crop Canvas
    - Display all available background colours
    - Exposure Gallery v1.0
    - JPEG Directory Thumbnail system (Apect ratio)
    - JPEG Directory thumbnail system
    - Change Background Color each day





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek