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

Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2009, 03: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, 11: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'], 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
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 07:49 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03949 seconds
  • Memory Usage 2,297KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete