Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2009, 02:24 PM
bananalive bananalive is offline
 
Join Date: Oct 2007
Location: UK
Posts: 2,802
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Image upload & re-size

I'm using the following code to upload & re-size images. However it isn't resizing them. Any ideas?

PHP Code:
    require_once(DIR . '/includes/class_upload.php');
    require_once(DIR . '/includes/class_image.php');

    $upload = new vB_Upload_Image($vbulletin);
    $upload->image =& vB_Image::fetch_library($vbulletin);
    $upload->path = $vbulletin->GPC['imagespath'];
    $upload->maxwidth = 120;
    $upload->maxheight = 120;
    $imagepath = $upload->process_upload($vbulletin->GPC['upload'])); 
Thanks,
Reply With Quote
  #2  
Old 08-05-2009, 10:18 PM
mad@Max's Avatar
mad@Max mad@Max is offline
 
Join Date: Jul 2007
Location: Russia
Posts: 536
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Was same problem https://vborg.vbsupport.ru/showthread.php?t=220191
And i found that class vB_Upload_Image is not complete, as stated in the documentation http://members.vbulletin.com/api/vBu...oad_Image.html
Original look
PHP Code:
class vB_Upload_Image extends vB_Upload_Abstract
{
    /**
    * Path that uploaded image is to be saved to
    *
    * @var    string
    */
    var $path = '';

    function is_valid_extension($extension)
    {
        return !empty($this->image->info_extensions["{$this->upload['extension']}"]);
    }

    function process_upload($uploadurl = '')
    {
        if ($uploadurl == '' OR $uploadurl == 'http://www.')
        {
            $uploadstuff =& $this->registry->GPC['upload'];
        }
        else
        {
            if (is_uploaded_file($this->registry->GPC['upload']['tmp_name']))
            {
                $uploadstuff =& $this->registry->GPC['upload'];
            }
            else
            {
                $uploadstuff =& $uploadurl;
            }
        }

        if ($this->accept_upload($uploadstuff))
        {
            if ($this->image->is_valid_thumbnail_extension(file_extension($this->upload['filename'])))
            {
                if ($this->imginfo = $this->image->fetch_image_info($this->upload['location']))
                {
                    if (!$this->image->fetch_must_convert($this->imginfo[2]))
                    {
                        if (!$this->imginfo[2])
                        {
                            $this->set_error('upload_invalid_image');
                            return false;
                        }

                        if ($this->image->fetch_imagetype_from_extension($this->upload['extension']) != $this->imginfo[2])
                        {
                            $this->set_error('upload_invalid_image_extension', $this->imginfo[2]);
                            return false;
                        }
                    }
                    else
                    {
                        $this->set_error('upload_invalid_image');
                        return false;
                    }
                }
                else
                {
                    $this->set_error('upload_imageinfo_failed_x', htmlspecialchars_uni($this->image->fetch_error()));
                    return false;
                }
            }
            else
            {
                $this->set_error('upload_invalid_image');
                return false;
            }

            if (!$this->upload['filestuff'])
            {
                if (!($this->upload['filestuff'] = file_get_contents($this->upload['location'])))
                {
                    $this->set_error('upload_file_failed');
                    return false;
                }
            }
            @unlink($this->upload['location']);

            return $this->save_upload();
        }
        else
        {
            return false;
        }
    }

    function save_upload()
    {
        if (!is_writable($this->path) OR !($fp = fopen($this->path . '/' . $this->upload['filename'], 'wb')))
        {
            $this->set_error('invalid_file_path_specified');
            return false;
        }

        if (@fwrite($fp, $this->upload['filestuff']) === false)
        {
            $this->set_error('error_writing_x', $this->upload['filename']);
            return false;
        }

        @fclose($fp);
        return $this->path . '/' . $this->upload['filename'];
    }
} 
I assume
PHP Code:
class vB_Upload_Image extends vB_Upload_Abstract
{
    /**
    * Path that uploaded image is to be saved to
    *
    * @var    string
    */
    var $path = '';

    function is_valid_extension($extension)
    {
        return !empty($this->image->info_extensions["{$this->upload['extension']}"]);
    }

    function process_upload($uploadurl = '')
    {
        if ($uploadurl == '' OR $uploadurl == 'http://www.')
        {
            $uploadstuff =& $this->registry->GPC['upload'];
        }
        else
        {
            if (is_uploaded_file($this->registry->GPC['upload']['tmp_name']))
            {
                $uploadstuff =& $this->registry->GPC['upload'];
            }
            else
            {
                $uploadstuff =& $uploadurl;
            }
        }

        if ($this->accept_upload($uploadstuff))
        {
            if ($this->imginfo = $this->image->fetch_image_info($this->upload['location']))
            {
                if ($this->image->is_valid_thumbnail_extension(file_extension($this->upload['filename'])))
                {
                    if (!$this->imginfo[2])
                    {
                        $this->set_error('upload_invalid_image');
                        return false;
                    }

                    if ($this->image->fetch_imagetype_from_extension($this->upload['extension']) != $this->imginfo[2])
                    {
                        $this->set_error('upload_invalid_image_extension', $this->imginfo[2]);
                        return false;
                    }
                }
                else
                {
                    $this->set_error('upload_invalid_image');
                    return false;
                }

                if ($this->allowanimation === false AND $this->imginfo[2] == 'GIF' AND $this->imginfo['animated'])
                {
                    $this->set_error('upload_invalid_animatedgif');
                    return false;
                }

                if (($this->maxwidth AND $this->imginfo[0] > $this->maxwidth) OR ($this->maxheight AND $this->imginfo[1] > $this->maxheight) OR $this->image->fetch_must_convert($this->imginfo[2]))
                {
                    // shrink-a-dink a big fat image or an invalid image for browser display (PSD, BMP, etc)
                    $this->upload['resized'] = $this->image->fetch_thumbnail($this->upload['filename'], $this->upload['location'], $this->maxwidth, $this->maxheight, $this->registry->options['thumbquality'], false, false, false, false);
                    if (empty($this->upload['resized']['filedata']))
                    {
                        $this->set_error('upload_exceeds_dimensions', $this->maxwidth, $this->maxheight, $this->imginfo[0], $this->imginfo[1]);
                        return false;
                    }
                    $jpegconvert = true;
                }
            }
            else
            {
                if ($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel'])
                {
                    $this->set_error('upload_imageinfo_failed_x', htmlspecialchars_uni($this->image->fetch_error()));
                }
                else
                {
                    $this->set_error('upload_invalid_file');
                }
                return false;
            }

            if ($this->maxuploadsize > 0 AND (!$this->fetch_best_resize($jpegconvert)))
            {
                return false;
            }

            if (!empty($this->upload['resized']))
            {
                if (!empty($this->upload['resized']['filedata']))
                {
                    $this->upload['filestuff'] =& $this->upload['resized']['filedata'];
                    $this->upload['filesize'] =& $this->upload['resized']['filesize'];
                    $this->imginfo[0] =& $this->upload['resized']['width'];
                    $this->imginfo[1] =& $this->upload['resized']['height'];
                }
                else
                {
                    $this->set_error('upload_exceeds_dimensions', $this->maxwidth, $this->maxheight, $this->imginfo[0], $this->imginfo[1]);
                    return false;
                }
            }
            else if (!($this->upload['filestuff'] = @file_get_contents($this->upload['location'])))
            {
                $this->set_error('upload_file_failed');
                return false;
            }
            @unlink($this->upload['location']);

            return $this->save_upload();
        }
        else
        {
            return false;
        }
    }

    function save_upload()
    {
        if (!is_writable($this->path) OR !($fp = fopen($this->path . '/' . $this->upload['filename'], 'wb')))
        {
            $this->set_error('invalid_file_path_specified');
            return false;
        }

        if (@fwrite($fp, $this->upload['filestuff']) === false)
        {
            $this->set_error('error_writing_x', $this->upload['filename']);
            return false;
        }

        @fclose($fp);
        return $this->path . '/' . $this->upload['filename'];
    }
} 
judging by the other classes.
Now $upload->maxwidth and $upload->maxheight works
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:37 AM.


Powered by vBulletin® Version 3.9.0
Copyright ©2000 - 2017, vBulletin Solutions Inc.