Example #1
0
/**
 * Photo Promenade -- a tiny PHP photo gallery
 * 
 * Helper functions, mainly from Drupal
 * http://drupal.org
 *
 * @author Peter Gassner <*****@*****.**>
 * @copyright Copyright 2008 Peter Gassner
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPLv3
 */
function scale($source, $target, $size)
{
    $width = array_key_exists('width', $size) ? $size['width'] : -1;
    $height = array_key_exists('height', $size) ? $size['height'] : -1;
    if (gd_available()) {
        image_scale($source, $target, $width, $height);
    }
}
/**
 * Save file uploaded by user and generate setting to save.
 *
 * @param <file> $file
 *    File uploaded from user
 *
 * @param <string> $banner_folder
 *    Folder where save image
 *
 * @param <string> $banner_thumb_folder
 *    Folder where save image thumbnail
 *
 * @return <array>
 *    Array with file data.
 *    FALSE on error.
 */
function _black_lagoon_save_image($file, $banner_folder = 'public://blacklagoon-banner/', $banner_thumb_folder = 'public://blacklagoon-banner/thumb/')
{
    // Check directory and create it (if not exist)
    _black_lagoon_check_dir($banner_folder);
    _black_lagoon_check_dir($banner_thumb_folder);
    $parts = pathinfo($file->filename);
    $destination = $banner_folder . $parts['basename'];
    $setting = array();
    $file->status = FILE_STATUS_PERMANENT;
    // Copy temporary image into banner folder
    if ($img = file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
        // Generate image thumb
        $image = image_load($destination);
        $small_img = image_scale($image, 300, 100);
        $image->source = $banner_thumb_folder . $parts['basename'];
        image_save($image);
        // Set image info
        $setting['image_path'] = $destination;
        $setting['image_thumb'] = $image->source;
        $setting['image_url'] = '<front>';
        $setting['image_title'] = '';
        $setting['image_weight'] = '';
        $setting['image_visibility'] = '*';
        return $setting;
    }
    return FALSE;
}
Example #3
0
function _agency_1_theme_settings_map($form, &$form_state)
{
    $validators = array('file_validate_is_image' => array());
    $file = file_save_upload('background_file', $validators);
    $map_file = file_save_upload('footer_contact_us_map_image', $validators);
    //Map image
    if (isset($map_file)) {
        // File upload was attempted.
        if ($map_file) {
            // Move the file, into the Drupal file system
            if ($map_filename = file_unmanaged_copy($map_file->uri, 'public://background_images', FILE_EXISTS_RENAME)) {
                //resize, if necessary
                $map_resized = image_load($map_file->uri);
                list($map_width, $map_height) = getimagesize($map_file->uri);
                if ($map_width > 150 || $map_height > 150) {
                    if ($map_width / $map_height >= 1) {
                        image_scale($map_resized, 150);
                    } else {
                        image_scale($map_resized, null, 150);
                    }
                    image_save($map_resized, $map_filename, FILE_EXISTS_RENAME);
                    $map_resized->status = FILE_STATUS_PERMANENT;
                    drupal_set_message('Uploaded image was greater than 150px.  Image has been resized.');
                }
                //end resize
                unset($form['footer_contact_us']['settings']['footer_contact_us_map_path']);
                $form['footer_contact_us']['settings']['footer_contact_us_map_path']['#value'] = $map_filename;
                $form['footer_contact_us_map_path']['#parents'] = array('footer_contact_us_map_path');
                form_set_value($form['footer_contact_us_map_path'], file_create_url($map_filename), $form_state);
                watchdog('Theme Settings', 'New Footer Map Image uploaded: ' . file_create_url($map_filename));
                drupal_set_message('Map Image uploaded.  URL: ' . file_create_url($map_filename));
            } else {
                form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
            }
        } else {
            // File upload failed.
            form_set_error('background_file', t('The image could not be uploaded.'));
        }
    }
}
Example #4
0
/**
 * handle uploaded product images
 * @author newnius
 * @return string 'true' or reasons
 *
 */
function upload_image($image)
{
    /* check whether file is uploaded successfully
     */
    if ($image['error'] > 0) {
        switch ($image['error']) {
            case 1:
                //size is larger than upload_max_filesize in php.ini
                return 'false';
                //return '文件大小超过配置文件设置';
            //return '文件大小超过配置文件设置';
            case 2:
                //size is larger than MAX_FILE_SIZE in the form
                return 'false';
                //return '文件大小超过限制';
            //return '文件大小超过限制';
            case 3:
                //file is not uploaded completely
                return 'false';
                //return '图片没有被完整上传,请重试';
            //return '图片没有被完整上传,请重试';
            case 4:
                //file is not uploaded
                return 'false';
                //return '图片没有被上传,请重试';
            //return '图片没有被上传,请重试';
            case 6:
                //tmp folder is not found
                return 'false';
                //return '严重错误,找不到tmp目录';
            //return '严重错误,找不到tmp目录';
            case 7:
                //fail to write file
                return 'false';
                //return '严重错误,没有写权限';
            //return '严重错误,没有写权限';
            default:
                return 'false';
                //return '未知错误';
        }
    }
    /* check whether file extension is acceptable
     */
    $img_allowed_list = json_decode(IMG_ALLOWED_LIST);
    //echo $image['type'];
    if (in_array($image['type'], $img_allowed_list)) {
        /* check size
         */
        if ($image['size'] > LASTSIZEOFIMAGE * 1024) {
            return 'false';
            //return '图片大小不得超过'.LASTSIZEOFIMAGE.'KB。';
        }
        /* rename file
         */
        $name = date('Ymdhis') . rand(100, 999) . '.' . pathinfo($image['name'], PATHINFO_EXTENSION);
        do {
            $name = date('Ymdhis') . rand(100, 999) . '.' . pathinfo($image['name'], PATHINFO_EXTENSION);
        } while (file_exists(UPLOADED_FILE_FOLDER . $name));
        /* move file
         */
        //echo $image['tmp_name'];
        if (move_uploaded_file($image['tmp_name'], UPLOADED_FILE_FOLDER . $name)) {
            //echo $name;
            image_scale(UPLOADED_FILE_FOLDER . $name, 186, 186, 's_');
            image_scale(UPLOADED_FILE_FOLDER . $name, 400, 400, 'm_');
            return $name;
        } else {
            //echo 'eaea';
            return 'false';
            //return '严重错误,无法转存图片';
        }
    } else {
        return 'false';
        //echo '123';
        //return '不支持的文件类型';
    }
}