PHP Uploading Help

Discussion in 'PHP' started by Pixel T., Feb 12, 2009.

  1. #1
    I recently hired a DP member to code me a script for one of my sites. It involves uploading images. I was wondering if there was any way that I could add something to the script that would make it automatically resize an image to a specific width and height.

    Thanks,
     
    Pixel T., Feb 12, 2009 IP
  2. diligenthost

    diligenthost Peon

    Messages:
    685
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you trying to make thumbnails, or does the script already create thumbnails? If so, all you should have to do is change the size of the thumbs you want (should in the script somewhere). Otherwise you'll have to implement an image resizing script and code it up with your existing script if you want it to be automated.

    I'd say the best person for the job is the person who coded it in the first place, if I were you I'd tackle it myself though ;)
     
    diligenthost, Feb 12, 2009 IP
  3. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #3
    well since the images were uploaded huge, the thumbnail, even when they are small, take forever to load.

    The person that did the script isnt replying for the past 6 days.

    I'm trying to tackle it myself and thats why I'm here :p
     
    Pixel T., Feb 12, 2009 IP
  4. bozy12v

    bozy12v Active Member

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    You can find free script that resize images. All you need to do it's to change the way you address those images to something like this:
    <img src="imgresize.php?w=400&h=600&img=$image_path">
     
    bozy12v, Feb 13, 2009 IP
  5. manjifera

    manjifera Well-Known Member

    Messages:
    232
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    118
    #5
    are you looking for help or to do work for you? if its work do let me know will do this with thumbs cache method!
    as practically no one will work for free :|
     
    manjifera, Feb 13, 2009 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    <?php
    
    class Manipulation
    {
        public $image;
    
        const DEGREES = 1;
        const RADIANS = 2;
        const GRADS = 3;
        const DIMENSIONS = 1;
        const PERCENT = 2;
        const FRACTION = 3;
    
        public function __construct( &$image = null )
        {
            if ( !is_null($image) and $this->_checkImage($image) )
                $this->setImage( $image );
        }
    
        public function setImage( &$image )
        {
            $this->image = &$image;
        }
    
        public function rotate( $angle, $bg_colour = 0, $ignore_transparent = 0, $measuring_style = self::DEGREES )
        {
            $this->_checkImage();
            if ( $measuring_style != self::DEGREES )
                $angle = self::_convert( $angle, $measuring_style );
            $this->image = imagerotate( $this->image, $angle, $bg_colour, 0 );
        }
    
        public function crop( $x = 10, $y = 100, $width = 400, $height = 400 )
        {
            if ( $width > $this->_getImageWidth() )
                $width = $this->_getImageWidth();
            if ( $height > $this->_getImageHeight() )
                $height = $this->_getImageHeight();
            $temp = imagecreatetruecolor( $width, $height );
            imagecopy( $temp, $this->image, 0, 0, $x, $y, $width, $height );
            $this->image = $temp;
            unset( $temp );
        }
    
        public function border( $px = 5, $colour = array('255', '255', '255') )
        {
            $img_width = $this->_getImageWidth() + ( 2 * $px );
            $img_height = $this->_getImageHeight() + ( 2 * $px );
            $temp = imagecreate( $img_width, $img_height );
            $border = imagecolorallocate( $temp, $colour[0], $colour[1], $colour[2] );
            if ( !is_array($colour) )
                $border = imagecolorallocatealpha( $temp, 0, 0, 0, 127 );
            else
                $border = imagecolorallocate( $temp, ($colour[0]) ? $colour[0] : 0, ($colour[1]) ? $colour[1] : 0, ($colour[2]) ? $colour[2] : 0 );
            imagefilledrectangle( $temp, 0, 0, $img_width, $img_height, $border );
            imagecopy( $temp, $this->image, $px, $px, 0, 0, $this->_getImageWidth(), $this->_getImageHeight() );
            $this->image = $temp;
            unset( $temp );
        }
    
        public function resize( $to = array('height' => 100, 'width' => 100), $type = self::DIMENSIONS )
        {
            $width = $this->_getImageWidth();
            $height = $this->_getImageHeight();
            if ( $type == self::PERCENT )
                $to = $to / 100;
            if ( $type == self::PERCENT or $type == self::FRACTION )
            {
                $new_width = $width * $to;
                $new_height = $height * $to;
            }
            if ( $type == self::DIMENSIONS )
            {
                $new_width = isset( $to['width'] ) ? $to['width'] : $to[0];
                $new_height = isset( $to['height'] ) ? $to['height'] : $to[1];
            }
            $temp = imagecreatetruecolor( $new_width, $new_height );
            imagecopyresampled( $temp, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
            $this->image = $temp;
            unset( $temp );
            return $this;
        }
    
        private function _checkImage( &$image = null )
        {
            if ( !is_resource(is_null($image) ? $this->image : $image) or get_resource_type(is_null($image) ? $this->image : $image) != 'gd' )
                throw new Exception( 'No image provided.' );
            return true;
        }
    
        private function _convert( $angle, $measuring_style )
        {
            if ( $measuring_style == self::RADIANS )
                $angle = ( (180 / pi()) * $angle );
            if ( $measuring_style == self::GRADS )
                $angle = ( 9 / 10 ) * $angle;
            return $angle % 360;
        }
    
        private function _getImageWidth()
        {
            return ( int )imagesx( $this->image );
        }
    
        private function _getImageHeight()
        {
            return ( int )imagesy( $this->image );
        }
    }
    PHP:
     
    Danltn, Feb 13, 2009 IP