× Warning! This forum is in archival status. New contributions may not work.
Image not checked
  • If a user uploads an incorrect image, image is stored in database in database in Images table, but throws error. In Images table the row have null in col size (width, height), UploadedByUser, Date, ContentType, and empty string in ImageFileName and ThumbnailFileName.

    After this problem user can not upload new image, edit images, list images or delete image.

    Where can I check validity of image before store it in database?
  • 2 Comments sorted by
  • hi, that would go in classes/Image.php method OnImageUpload
  • If someone have a same problems with uploaded images this is the solution:

    There is the modified code of OnImageUpload function:

        public static function OnImageUpload($p_fileVar, $p_attributes,
                                             $p_userId = null, $p_id = null,
                                             $p_isLocalFile = false)
        {
            global $Campsite;
            if (function_exists("camp_load_translation_strings")) {
                camp_load_translation_strings("api");
            }

            if (!is_array($p_fileVar)) {
                return new PEAR_Error("Invalid arguments given to Image::OnImageUpload()");
            }

            // Verify its a valid image file.
            $imageInfo = @getimagesize($p_fileVar['tmp_name']);
            if ($imageInfo === false) {
                return new PEAR_Error(getGS("The file uploaded is not an image."));
            }
            $extension = Image::__ImageTypeToExtension($imageInfo[2]);
           
            // Validate Photo
            $createMethodName = Image::__GetImageTypeCreateMethod($imageInfo[2]);
            if (!isset($createMethodName)) {
                return new PEAR_Error(getGS("Image type $1 is not supported.",
                image_type_to_mime_type($imageInfo[2])));
            }
            $imageHandler = @$createMethodName($p_fileVar['tmp_name']);
            if (!$imageHandler) {
                return new PEAR_Error(getGS("Uploaded image $1 appears to be corrupt, unable to store on system.",
                $p_fileVar['tmp_name']));
            }
            unset($imageHandler,$createMethodName);
           
            // Check if image & thumbnail directories are writable.
            $imageDir = $Campsite['IMAGE_DIRECTORY'];
            $thumbDir = $Campsite['THUMBNAIL_DIRECTORY'];
            if (!file_exists($imageDir) || !is_writable($imageDir)) {
                return new PEAR_Error(camp_get_error_message(CAMP_ERROR_WRITE_DIR, $imageDir),
                                      CAMP_ERROR_WRITE_DIR);
            }
            if (!file_exists($thumbDir) || !is_writable($thumbDir)) {
                return new PEAR_Error(camp_get_error_message(CAMP_ERROR_WRITE_DIR, $thumbDir),
                                      CAMP_ERROR_WRITE_DIR);
            }

            // Are we updating or creating?
             if (!is_null($p_id)) {
                 // Updating the image
                 $image = new Image($p_id);
                 $image->update($p_attributes, false);
                // Remove the old image & thumbnail because
                // the new file may have a different file extension.
                if (file_exists($image->getImageStorageLocation())) {
                    unlink($image->getImageStorageLocation());
                }
                if (file_exists($image->getThumbnailStorageLocation())) {
                    unlink($image->getThumbnailStorageLocation());
                }
            } else {
                // Creating the image
                $image = new Image();
                $image->create($p_attributes);
                $image->setProperty('TimeCreated', 'NULL', true, true);
                $image->setProperty('LastModified', 'NULL', true, true);
            }
            $image->setProperty('Location', 'local', false);
            // If we are using PHP version >= 4.3
            if (isset($imageInfo['mime'])) {
                $image->setProperty('ContentType', $imageInfo['mime'], false);
            } else {
                $image->setProperty('ContentType', $p_fileVar['type'], false);
            }
            if (!is_null($p_userId)) {
                $image->setProperty('UploadedByUser', $p_userId, false);
            }
            if (!isset($p_attributes['Date'])) {
                $image->setProperty('Date', 'NOW()', true, true);
            }
            $target = $image->generateImageStorageLocation($extension);
            $thumbnail = $image->generateThumbnailStorageLocation($extension);
            $image->setProperty('ImageFileName', basename($target), false);
            $image->setProperty('ThumbnailFileName', basename($thumbnail), false);

            try {
                if ($p_isLocalFile) {
                    if (!copy($p_fileVar['tmp_name'], $target)) {
                        throw new Exception(camp_get_error_message(CAMP_ERROR_CREATE_FILE, $target),
                                            CAMP_ERROR_CREATE_FILE);
                    }
                } else {
                    if (!rename($p_fileVar['tmp_name'], $target)) {
                        throw new Exception(camp_get_error_message(CAMP_ERROR_CREATE_FILE, $target),
                                            CAMP_ERROR_CREATE_FILE);
                    }
                }

                self::chmod($target, 0644);

                $createMethodName = Image::__GetImageTypeCreateMethod($imageInfo[2]);
                if (!isset($createMethodName)) {
                    throw new Exception(getGS("Image type $1 is not supported.",
                                        image_type_to_mime_type($p_imageType)));
                }

                $imageHandler = $createMethodName($target);
                if (!$imageHandler) {
                    throw new Exception(camp_get_error_message(CAMP_ERROR_UPLOAD_FILE, $p_fileVar['name']), CAMP_ERROR_UPLOAD_FILE);
                }

                $thumbnailImage = Image::ResizeImage($imageHandler,
                    $Campsite['THUMBNAIL_MAX_SIZE'], $Campsite['THUMBNAIL_MAX_SIZE']);
                if (PEAR::isError($thumbnailImage)) {
                    throw new Exception($thumbnailImage->getMessage(), $thumbnailImage->getCode());
                }

                $result = Image::SaveImageToFile($thumbnailImage, $thumbnail, $imageInfo[2]);
                if (PEAR::isError($result)) {
                    throw new Exception($result->getMessage(), $result->getCode());
                }

                self::chmod($thumbnail, 0644);
            } catch (Exception $ex) {
                if (file_exists($target)) {
                    @unlink($target);
                }
                if (file_exists($thumbnail)) {
                    @unlink($thumbnail);
                }
                if (is_null($p_id)) {
                    $image->delete();
                }
                return new PEAR_Error($ex->getMessage(), $ex->getCode());
            }

            $user = Zend_Registry::get('container')->getService('user')->getCurrentUser();
            if ($user && $user->isAdmin()) {
                $image->m_data['Status'] = 'approved';
                $image->m_data['Source'] = 'local';
            }

            $image->commit();

            return $image;
        } // fn OnImageUpload


    With modification if one image broken or not uploaded properly, that not stored in database. (The new code is bolded)

    Post edited by Zsolt Baji at 2013-02-11 11:05:42