Announcement

Collapse
No announcement yet.

OnValidate validating Image...(?)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • OnValidate validating Image...(?)

    Hi,
    I'm trying to re-size an image before it's being saved to database (or image-folder). i have the code working for the re-sizing part. The question is when can I do it?

    If I place my code in the OnValidate event the data appears to be save already!?

    1. I click the upload button and select my image.

    2. The image get's uploaded to the tempfolder...
    2a. If I delete the file and press save I get an error and the image does not get saved (Logical, i'ts deleted).
    2b. If I put my resize code in OnBeforeUpdate or in OnValidate, overwriting the image, and save the image is it's original size!
    2c. Any other changed value here is stored... but not the image!

    Is the image already saved by this time?
    Why is there no way to put Ajax-events on image fields?
    I'm I missing something?

    Thank you!

  • #2
    Hi. Where is the resizing code?

    Comment


    • #3
      Originally posted by MikeDE View Post
      Hi. Where is the resizing code?
      The Code is in a library and the image gets resized so it works... The problem is that the image is already saved to DB or image path when OnValidate and BeforeUpdate gets fired (I believe)... All other values of fields can be changed and saved but not the image.
      Thanks!

      Comment


      • #4
        I was thinking if u can share it so guys might be able to test the case? at least..

        Comment


        • #5
          Originally posted by MikeDE View Post
          I was thinking if u can share it so guys might be able to test the case? at least..

          OnValidate ("pdavatar" being the name of the field, replace with yours):
          PHP Code:
          $filename $this->Ini->root $this->Ini->path_imag_temp '/' $this->NM_ajax_info['param']['pdavatar_ul_name'];

          if (
          $this->NM_ajax_info['param']['pdavatar_ul_name'] != '')
           {     
             
          $image = new SimpleImage();
             
          $image->load($filename);
             
          $image->scale(50);
             
          $image->save($filename);
           } 

          Library:

          PHP Code:
          <?php
          /* 
          * File: SimpleImage.php 
          * Author: Simon Jarvis 
          * Copyright: 2006 Simon Jarvis 
          * Date: 08/11/06 
          * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php 
          * This program is free software; you can redistribute it and/or 
          * modify it under the terms of the GNU General Public License 
          * as published by the Free Software Foundation; either version 2 
          * of the License, or (at your option) any later version. 
          * This program is distributed in the hope that it will be useful, 
          * but WITHOUT ANY WARRANTY; without even the implied warranty of 
          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
          * GNU General Public License for more details: 
          * http://www.gnu.org/licenses/gpl.html 
          */

           
          class SimpleImage {
           
             var 
          $image;
             var 
          $image_type;
           
             function 
          load($filename) {
           
                
          $image_info getimagesize($filename);
                
          $this->image_type $image_info[2];
                if( 
          $this->image_type == IMAGETYPE_JPEG ) {
           
                   
          $this->image imagecreatefromjpeg($filename);
                } elseif( 
          $this->image_type == IMAGETYPE_GIF ) {
           
                   
          $this->image imagecreatefromgif($filename);
                } elseif( 
          $this->image_type == IMAGETYPE_PNG ) {
           
                   
          $this->image imagecreatefrompng($filename);
                }
             }
              
              
              
             function 
          save($filename$image_type=IMAGETYPE_JPEG$compression=75$permissions=null) {
           
                if( 
          $image_type == IMAGETYPE_JPEG ) {
                   
          imagejpeg($this->image,$filename,$compression);
                } elseif( 
          $image_type == IMAGETYPE_GIF ) {
           
                   
          imagegif($this->image,$filename);
                } elseif( 
          $image_type == IMAGETYPE_PNG ) {
           
                   
          imagepng($this->image,$filename);
                }
                if( 
          $permissions != null) {
           
                   
          chmod($filename,$permissions);
                }
             }
             function 
          output($image_type=IMAGETYPE_JPEG) {
           
                if( 
          $image_type == IMAGETYPE_JPEG ) {
                   
          imagejpeg($this->image);
                } elseif( 
          $image_type == IMAGETYPE_GIF ) {
           
                   
          imagegif($this->image);
                } elseif( 
          $image_type == IMAGETYPE_PNG ) {
           
                   
          imagepng($this->image);
                }
             }
             function 
          getWidth() {
           
                return 
          imagesx($this->image);
             }
             function 
          getHeight() {
           
                return 
          imagesy($this->image);
             }
             function 
          resizeToHeight($height) {
           
                
          $ratio $height $this->getHeight();
                
          $width $this->getWidth() * $ratio;
                
          $this->resize($width,$height);
             }
           
             function 
          resizeToWidth($width) {
                
          $ratio $width $this->getWidth();
                
          $height $this->getheight() * $ratio;
                
          $this->resize($width,$height);
             }
           
             function 
          scale($scale) {
                
          $width $this->getWidth() * $scale/100;
                
          $height $this->getheight() * $scale/100;
                
          $this->resize($width,$height);
             }
           
             function 
          resize($width,$height) {
                
          $new_image imagecreatetruecolor($width$height);
                
          imagecopyresampled($new_image$this->image0000$width$height$this->getWidth(), $this->getHeight());
                
          $this->image $new_image;
             }      
           
          }
          ?>
          Last edited by HakLin0816; 10-09-2014, 07:18 PM.

          Comment


          • #6
            you are right, seems something related to this image type perhaps is a bug, are you using sc7 or 8? I tried on 7.

            Comment


            • #7
              I'm using 8...

              Comment


              • #8
                seems same stuff again in sc7 and 8
                i was searching, found this post and a response from Albert Aduom, have a look
                http://www.scriptcase.net/forum/show...t=resize+image

                Comment


                • #9
                  Thank you MikeDE,
                  I read that one also, but I think they are under the impression that OnValidate works as with other field types!?
                  The problem is that I wan't to validate the image BEFORE it gets stored. As far I as I can see there is no event available to do this as there are no AJAX-Events on image fields and OnValidate does not work (as expected)...

                  Comment


                  • #10
                    So this is ok then or are someone looking into this?

                    Comment


                    • #11
                      Originally posted by HakLin0816 View Post
                      So this is ok then or are someone looking into this?
                      no idea, but i guess it needs some gurus to test it and provide alternatives, before bartho appears then you will need to hibernate

                      hmmm why don't you start your project and keep this later for update in next releases? have you server site limit issues?

                      don't know if this applies or help, but i once couldn't manage to rename the image due to such behavior (even was looking so logic and code was ok), however, aim was not to override other images on the server, so i used the sub-folder setting it was useful

                      Comment


                      • #12
                        Do you know how to manually store the resized image in database?
                        Thank you.

                        Comment


                        • #13
                          hi
                          no, but try sending it within code
                          in proper event, you will see from the right side bunch of code, select "insert records in another table", this one i used, including image(database) blob file is being stored, try send your variable using that code to a blob field in a table, see what happened

                          Comment


                          • #14
                            Hi HakLin0816,

                            Did you ever get this image resizing function to work using the SimpleImage.php library?

                            Comment


                            • #15
                              Would also be interested in that ...

                              Comment

                              Working...
                              X