vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Image upload & re-size (https://vborg.vbsupport.ru/showthread.php?t=216730)

bananalive 06-21-2009 03:24 PM

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,

mad@Max 08-05-2009 11:18 PM

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'], falsefalsefalsefalse);
                    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 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 :)


All times are GMT. The time now is 09:13 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01186 seconds
  • Memory Usage 1,839KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (2)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete