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!

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!

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):


$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
/* 
* 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->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      
 
}
?>

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

I’m using 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/showthread.php?5247-sc7-1-image-processing-(resize-or-change-dpi)-after-upload-is-it-possible&highlight=resize+image

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)…

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 :slight_smile: then you will need to hibernate :smiley:

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

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

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

Hi HakLin0816,

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

Would also be interested in that …

If I use this

$filename = $this->Ini->root . $this->Ini->path_imag_temp . ‘/’ . $this->NM_ajax_info[‘param’][‘fieldname_ul_name’];

I get an error message when trying to save record

“Undefined index: fieldname_ul_name”

I also tried {fieldname} but got the same error. Anyway the record (image) is saved. But the resize script doesn’t work.

Any ideas?

Undefined index means you are trying to access an index of an array that doesn’t exists. Taking in consideration script works, is not working because you are not pointing to the file. I don’t use images stored in database, I’m not sure if fieldname will store the oath to temp file. Vardump the field to know exactly wich value stores