示例#1
0
function fn_attach_images($body, &$mailer)
{
    $http_location = Registry::get('config.http_location');
    $https_location = Registry::get('config.https_location');
    $http_path = Registry::get('config.http_path');
    $https_path = Registry::get('config.https_path');
    $files = array();
    if (preg_match_all("/(?<=\\ssrc=|\\sbackground=)('|\")(.*)\\1/SsUi", $body, $matches)) {
        $files = fn_array_merge($files, $matches[2], false);
    }
    if (preg_match_all("/(?<=\\sstyle=)('|\").*url\\(('|\"|\\\\\\1)(.*)\\2\\).*\\1/SsUi", $body, $matches)) {
        $files = fn_array_merge($files, $matches[3], false);
    }
    if (empty($files)) {
        return $body;
    } else {
        $files = array_unique($files);
        foreach ($files as $k => $_path) {
            $cid = 'csimg' . $k;
            $path = str_replace('&amp;', '&', $_path);
            $real_path = '';
            // Replace url path with filesystem if this url is NOT dynamic
            if (strpos($path, '?') === false && strpos($path, '&') === false) {
                if (($i = strpos($path, $http_location)) !== false) {
                    $real_path = substr_replace($path, DIR_ROOT, $i, strlen($http_location));
                } elseif (($i = strpos($path, $https_location)) !== false) {
                    $real_path = substr_replace($path, DIR_ROOT, $i, strlen($https_location));
                } elseif (!empty($http_path) && ($i = strpos($path, $http_path)) !== false) {
                    $real_path = substr_replace($path, DIR_ROOT, $i, strlen($http_path));
                } elseif (!empty($https_path) && ($i = strpos($path, $https_path)) !== false) {
                    $real_path = substr_replace($path, DIR_ROOT, $i, strlen($https_path));
                }
            }
            if (empty($real_path)) {
                $real_path = strpos($path, '://') === false ? $http_location . '/' . $path : $path;
            }
            list($width, $height, $mime_type) = fn_get_image_size($real_path);
            if (!empty($width)) {
                $cid .= '.' . fn_get_image_extension($mime_type);
                $content = fn_get_contents($real_path);
                $mailer->AddImageStringAttachment($content, $cid, 'base64', $mime_type);
                $body = preg_replace("/(['\"])" . str_replace("/", "\\/", preg_quote($_path)) . "(['\"])/Ss", "\\1cid:" . $cid . "\\2", $body);
            }
        }
    }
    return $body;
}
示例#2
0
/**
 * Resizes image
 * @param string $src source image path
 * @param integer $new_width new image width
 * @param integer $new_height new image height
 * @param string $bg_color new image background color
 * @param array $custom_settings custom convertion settings
 * @return array - new image contents and format
 */
function fn_resize_image($src, $new_width = 0, $new_height = 0, $bg_color = '#ffffff', $custom_settings = array())
{
    static $notification_set = false;
    static $gd_settings = array();
    if (empty($gd_settings)) {
        $gd_settings = Settings::instance()->getValues('Thumbnails');
    }
    $settings = !empty($custom_settings) ? $custom_settings : $gd_settings;
    $class = '\\Tygh\\Backend\\Images\\' . ucfirst(Registry::ifGet('config.tweaks.image_resize_lib', 'gd'));
    if (file_exists($src) && (!empty($new_width) || !empty($new_height))) {
        $img_functions = $class::supportedFormats();
        list($width, $height, $mime_type) = fn_get_image_size($src);
        $ext = fn_get_image_extension($mime_type);
        if (empty($width) || empty($height) || empty($ext)) {
            return false;
        }
        if (empty($img_functions[$ext])) {
            if ($notification_set == false) {
                fn_set_notification('E', __('error'), __('error_image_format_not_supported', array('[format]' => $ext)));
                $notification_set = true;
            }
            return false;
        }
        if (empty($new_width) || empty($new_height)) {
            if ($width < $new_width) {
                $new_width = $width;
            }
            if ($height < $new_height) {
                $new_height = $height;
            }
        }
        $dst_width = $new_width;
        $dst_height = $new_height;
        $x = 0;
        $y = 0;
        if (empty($new_height)) {
            // if we passed width only, calculate height
            $dst_height = $new_height = $height / $width * $new_width;
        } elseif (empty($new_width)) {
            // if we passed height only, calculate width
            $dst_width = $new_width = $width / $height * $new_height;
        } else {
            // we passed width and height, we need to fit image in this sizes
            if ($new_width * $height / $width > $dst_height) {
                $new_width = $width * $dst_height / $height;
            }
            $new_height = $height / $width * $new_width;
            if ($new_height * $width / $height > $dst_width) {
                $new_height = $height * $dst_width / $width;
            }
            $new_width = $width / $height * $new_height;
            $x = intval(($dst_width - $new_width) / 2);
            $y = intval(($dst_height - $new_height) / 2);
        }
        $new_width = intval($new_width);
        $new_height = intval($new_height);
        if (!empty($bg_color) && !preg_match('/^#([0-9a-f]{3}){1,2}$/i', $bg_color)) {
            $bg_color = '#ffffff';
        }
        try {
            return $class::resize($src, array('ext' => $ext, 'new_width' => $new_width, 'new_height' => $new_height, 'dst_width' => $dst_width, 'dst_height' => $dst_height, 'width' => $width, 'height' => $height, 'bg_color' => $bg_color, 'convert_to' => $settings['convert_to'], 'jpeg_quality' => $settings['jpeg_quality'], 'x' => $x, 'y' => $y));
        } catch (Exception $e) {
            return array('', $ext);
        }
    }
    return false;
}
示例#3
0
/**
 * Resizes image
 * @param string $src source image path
 * @param integer $new_width new image width
 * @param integer $new_height new image height
 * @param string $bg_color new image background color
 * @param array $custom_settings custom convertion settings
 * @return array - new image contents and format
 */
function fn_resize_image($src, $new_width = 0, $new_height = 0, $bg_color = '#ffffff', $custom_settings = array())
{
    static $general_settings = array();
    if (empty($general_settings)) {
        $general_settings = Settings::instance()->getValues('Thumbnails');
    }
    gc_collect_cycles();
    $settings = empty($custom_settings) ? $general_settings : $custom_settings;
    /** @var \Imagine\Image\ImagineInterface $imagine */
    $imagine = Tygh::$app['image'];
    $format = $settings['convert_to'];
    if ($format === 'original') {
        if ($original_file_type = fn_get_image_extension(fn_get_mime_content_type($src, true))) {
            $format = $original_file_type;
        } else {
            $format = 'png';
        }
    }
    $transparency = null;
    if (empty($bg_color)) {
        $bg_color = '#FFF';
        if ($format == 'png' || $format == 'gif') {
            $transparency = 0;
        }
    } elseif (!preg_match('/^#([0-9a-f]{3}){1,2}$/i', $bg_color)) {
        $bg_color = '#FFF';
    }
    try {
        $image = $imagine->open($src);
        list($new_width, $new_height) = ImageHelper::originalProportionsFallback($image->getSize()->getWidth(), $image->getSize()->getHeight(), $new_width, $new_height);
        // This is a non-necessary operation
        // which can however trigger exceptions if isn't supported by a driver
        fn_catch_exception(function () use($image) {
            $image->usePalette(new \Imagine\Image\Palette\RGB());
        });
        $filter = $imagine instanceof \Imagine\Gd\Imagine ? \Imagine\Image\ImageInterface::FILTER_UNDEFINED : \Imagine\Image\ImageInterface::FILTER_LANCZOS;
        $new_size = new \Imagine\Image\Box($new_width, $new_height);
        $thumbnail = $image->thumbnail($new_size, \Imagine\Image\ImageInterface::THUMBNAIL_INSET, $filter);
        // In case that created thumbnail is smaller than required size, we create
        // an empty canvas of required size and center thumbnail on it
        $thumbnail_coordinates = new \Imagine\Image\Point((int) (($new_size->getWidth() - $thumbnail->getSize()->getWidth()) / 2), (int) (($new_size->getHeight() - $thumbnail->getSize()->getHeight()) / 2));
        if (!$image->palette()->supportsAlpha()) {
            $transparency = null;
        }
        $canvas_color = $image->palette()->color($bg_color, $transparency);
        $canvas = $imagine->create($new_size, $canvas_color);
        $canvas->paste($thumbnail, $thumbnail_coordinates);
        unset($thumbnail, $image);
        $thumbnail = $canvas;
        $options = array('jpeg_quality' => $settings['jpeg_quality'], 'png_compression_level' => 9, 'filter' => $filter, 'flatten' => true);
        $return = array($thumbnail->get($format, $options), $format);
        unset($thumbnail);
        gc_collect_cycles();
        return $return;
    } catch (\Exception $e) {
        $error_message = __('error_unable_to_create_thumbnail', array('[error]' => $e->getMessage(), '[file]' => $src));
        if (AREA == 'A') {
            fn_set_notification('E', __('error'), $error_message);
        }
        gc_collect_cycles();
        return false;
    }
}
示例#4
0
 function Create($sFilename = '')
 {
     // check for required gd functions
     if (!function_exists('imagecreate') || !function_exists("image{$this->sFileType}") || $this->vBackgroundImages != '' && !function_exists('imagecreatetruecolor')) {
         return false;
     }
     // get background image if specified and copy to CAPTCHA
     if (is_array($this->vBackgroundImages) || $this->vBackgroundImages != '') {
         // create new image
         $this->oImage = imagecreatetruecolor($this->iWidth, $this->iHeight);
         // create background image
         $src = is_array($this->vBackgroundImages) ? array_rand($this->vBackgroundImages) : $this->vBackgroundImages;
         list($_w, $_h, $_type, $_attr) = @getimagesize($src);
         $mime_type = image_type_to_mime_type($_type);
         $ext = fn_get_image_extension($mime_type);
         if ($ext == 'gif' && function_exists('imagecreatefromgif')) {
             $oBackgroundImage = imagecreatefromgif($src);
         } elseif ($ext == 'jpg' && function_exists('imagecreatefromjpeg')) {
             $oBackgroundImage = imagecreatefromjpeg($src);
         } elseif ($ext == 'png' && function_exists('imagecreatefrompng')) {
             $oBackgroundImage = imagecreatefrompng($src);
         } else {
             return false;
         }
         // copy background image
         imagecopy($this->oImage, $oBackgroundImage, 0, 0, 0, 0, $this->iWidth, $this->iHeight);
         // free memory used to create background image
         imagedestroy($oBackgroundImage);
     } else {
         // create new image
         $this->oImage = imagecreate($this->iWidth, $this->iHeight);
     }
     // allocate white background colour
     imagecolorallocate($this->oImage, 255, 255, 255);
     // check for owner text
     if ($this->sOwnerText != '') {
         $this->DrawOwnerText();
     }
     // check for background image before drawing lines
     if (!is_array($this->vBackgroundImages) && $this->vBackgroundImages == '') {
         $this->DrawLines();
     }
     $this->GenerateCode();
     $this->DrawCharacters();
     // write out image to file or browser
     $this->WriteFile($sFilename);
     // free memory used in creating image
     imagedestroy($this->oImage);
     return true;
 }
示例#5
0
function fn_add_product_options_files($product_data, &$cart, &$auth, $update = false, $location = 'cart')
{
    // Check if products have cusom images
    if (!$update) {
        $uploaded_data = fn_filter_uploaded_data('product_data');
    } else {
        $uploaded_data = fn_filter_uploaded_data('cart_products');
    }
    // Check for the already uploaded files
    if (!empty($product_data['custom_files']['uploaded'])) {
        foreach ($product_data['custom_files']['uploaded'] as $file_id => $file_data) {
            if (Storage::instance('images')->isExist('sess_data/' . fn_basename($file_data['path']))) {
                $id = $file_data['product_id'] . $file_data['option_id'] . $file_id;
                $uploaded_data[$id] = array('name' => $file_data['name'], 'path' => 'sess_data/' . fn_basename($file_data['path']));
                $product_data['custom_files'][$id] = $file_data['product_id'] . '_' . $file_data['option_id'];
            }
        }
    }
    if (!empty($uploaded_data) && !empty($product_data['custom_files'])) {
        $files_data = array();
        foreach ($uploaded_data as $key => $file) {
            $file_info = fn_pathinfo($file['name']);
            $file['extension'] = empty($file_info['extension']) ? '' : $file_info['extension'];
            $file['is_image'] = fn_get_image_extension($file['type']);
            $_data = explode('_', $product_data['custom_files'][$key]);
            $product_id = empty($_data[0]) ? 0 : $_data[0];
            $option_id = empty($_data[1]) ? 0 : $_data[1];
            $file_id = str_replace($option_id . $product_id, '', $key);
            if (empty($file_id)) {
                $files_data[$product_id][$option_id][] = $file;
            } else {
                $files_data[$product_id][$option_id][$file_id] = $file;
            }
        }
    }
    unset($product_data['custom_files']);
    foreach ($product_data as $key => $data) {
        $product_id = !empty($data['product_id']) ? $data['product_id'] : $key;
        // Check if product has cusom images
        if ($update || isset($files_data[$key])) {
            $hash = $key;
        } else {
            $hash = $product_id;
        }
        $_options = fn_get_product_options($product_id);
        if (!empty($files_data[$hash]) && is_array($files_data[$hash])) {
            foreach ($files_data[$hash] as $option_id => $files) {
                foreach ($files as $file_id => $file) {
                    // Check for the allowed extensions
                    if (!empty($_options[$option_id]['allowed_extensions'])) {
                        if (empty($file['extension']) && !empty($_options[$option_id]['allowed_extensions']) || !preg_match("/\\b" . $file['extension'] . "\\b/i", $_options[$option_id]['allowed_extensions'])) {
                            fn_set_notification('E', __('error'), $file['name'] . ': ' . __('text_forbidden_uploaded_file_extension', array('[ext]' => $file['extension'], '[exts]' => $_options[$option_id]['allowed_extensions'])));
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    // Check for the max file size
                    if (!empty($_options[$option_id]['max_file_size'])) {
                        if (empty($file['size'])) {
                            $file['size'] = filesize($file['path']);
                        }
                        if ($file['size'] > $_options[$option_id]['max_file_size'] * 1024) {
                            fn_set_notification('E', __('error'), $file['name'] . ': ' . __('text_forbidden_uploaded_file_size', array('[size]' => $_options[$option_id]['max_file_size'] . ' kb')));
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    $_file_path = 'sess_data/file_' . uniqid(TIME);
                    list(, $_file_path) = Storage::instance('custom_files')->put($_file_path, array('file' => $file['path']));
                    if (!$_file_path) {
                        fn_set_notification('E', __('error'), __('text_cannot_create_file', array('[file]' => $file['name'])));
                        unset($files_data[$hash][$option_id][$file_id]);
                        continue;
                    }
                    $file['path'] = $_file_path;
                    $file['file'] = fn_basename($file['path']);
                    if ($file['is_image']) {
                        $file['thumbnail'] = 'image.custom_image?image=' . $file['file'] . '&type=T';
                        $file['detailed'] = 'image.custom_image?image=' . $file['file'] . '&type=D';
                    }
                    $file['location'] = $location;
                    if ($update) {
                        $cart['products'][$key]['extra']['custom_files'][$option_id][] = $file;
                    } else {
                        $data['extra']['custom_files'][$option_id][] = $file;
                    }
                }
                if ($update) {
                    if (!empty($cart['products'][$key]['product_options'][$option_id])) {
                        $cart['products'][$key]['product_options'][$option_id] = md5(serialize($cart['products'][$key]['extra']['custom_files'][$option_id]));
                    }
                } else {
                    if (!empty($data['extra']['custom_files'][$option_id])) {
                        $data['product_options'][$option_id] = md5(serialize($data['extra']['custom_files'][$option_id]));
                    }
                }
            }
            // Check the required options
            if (empty($data['extra']['parent'])) {
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && !$update) {
                        if (empty($data['product_options'][$option['option_id']])) {
                            fn_set_notification('E', __('error'), __('product_cannot_be_added'));
                            unset($product_data[$key]);
                            return array($product_data, $cart);
                        }
                    }
                }
            }
        } else {
            if (empty($data['extra']['parent'])) {
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && empty($cart['products'][$hash]['extra']['custom_files'][$option['option_id']]) && empty($data['extra']['custom_files'][$option['option_id']])) {
                        fn_set_notification('E', __('error'), __('product_cannot_be_added'));
                        unset($product_data[$key]);
                        return array($product_data, $cart);
                    }
                }
            }
        }
        if ($update) {
            foreach ($_options as $option) {
                if ($option['option_type'] == 'F' && empty($cart['products'][$key]['extra']['custom_files'][$option['option_id']])) {
                    unset($cart['products'][$key]['extra']['custom_files'][$option['option_id']]);
                    unset($cart['products'][$key]['product_options'][$option['option_id']]);
                    unset($data['product_options'][$option['option_id']]);
                }
            }
        }
        if (isset($cart['products'][$key]['extra']['custom_files'])) {
            foreach ($cart['products'][$key]['extra']['custom_files'] as $option_id => $files) {
                foreach ($files as $file) {
                    $data['extra']['custom_files'][$option_id][] = $file;
                }
                $data['product_options'][$option_id] = md5(serialize($files));
            }
        }
        $product_data[$key] = $data;
    }
    return array($product_data, $cart);
}
示例#6
0
function fn_resize_image($src, $dest, $new_width = 0, $new_height = 0, $make_box = false, $bg_color = '#ffffff')
{
    static $notification_set = false;
    static $gd_settings = array();
    if (file_exists($src) && !empty($dest) && (!empty($new_width) || !empty($new_height)) && extension_loaded('gd')) {
        $img_functions = array('png' => function_exists('imagepng'), 'jpg' => function_exists('imagejpeg'), 'gif' => function_exists('imagegif'));
        if (empty($gd_settings)) {
            $gd_settings = fn_get_settings('Thumbnails');
        }
        $dst_width = $new_width;
        $dst_height = $new_height;
        list($width, $height, $mime_type) = fn_get_image_size($src);
        if (empty($width) || empty($height)) {
            return false;
        }
        if ($width < $new_width) {
            $new_width = $width;
        }
        if ($height < $new_height) {
            $new_height = $height;
        }
        if ($dst_height == 0) {
            // if we passed width only, calculate height
            $new_height = $dst_height = $height / $width * $new_width;
        } elseif ($dst_width == 0) {
            // if we passed height only, calculate width
            $new_width = $dst_width = $width / $height * $new_height;
        } else {
            // we passed width and height, limit image by height! (hm... not sure we need it anymore?)
            if ($new_width * $height / $width > $dst_height) {
                $new_width = $width * $dst_height / $height;
            }
            $new_height = $height / $width * $new_width;
            if ($new_height * $width / $height > $dst_width) {
                $new_height = $height * $dst_width / $width;
            }
            $new_width = $width / $height * $new_height;
        }
        $w = number_format($new_width, 0, ',', '');
        $h = number_format($new_height, 0, ',', '');
        $ext = fn_get_image_extension($mime_type);
        if (!empty($img_functions[$ext])) {
            if ($make_box) {
                $dst = imagecreatetruecolor($dst_width, $dst_height);
            } else {
                $dst = imagecreatetruecolor($w, $h);
            }
            if (function_exists('imageantialias')) {
                imageantialias($dst, true);
            }
        } elseif ($notification_set == false) {
            $msg = fn_get_lang_var('error_image_format_not_supported');
            $msg = str_replace('[format]', $ext, $msg);
            fn_set_notification('E', fn_get_lang_var('error'), $msg);
            $notification_set = true;
            return false;
        }
        if ($ext == 'gif' && $img_functions[$ext] == true) {
            $new = imagecreatefromgif($src);
        } elseif ($ext == 'jpg' && $img_functions[$ext] == true) {
            $new = imagecreatefromjpeg($src);
        } elseif ($ext == 'png' && $img_functions[$ext] == true) {
            $new = imagecreatefrompng($src);
        } else {
            return false;
        }
        // Set transparent color to white
        // Not sure that this is right, but it works
        // FIXME!!!
        // $c = imagecolortransparent($new);
        list($r, $g, $b) = fn_parse_rgb($bg_color);
        $c = imagecolorallocate($dst, $r, $g, $b);
        //imagecolortransparent($dst, $c);
        if ($make_box) {
            imagefilledrectangle($dst, 0, 0, $dst_width, $dst_height, $c);
            $x = number_format(($dst_width - $w) / 2, 0, ',', '');
            $y = number_format(($dst_height - $h) / 2, 0, ',', '');
        } else {
            imagefilledrectangle($dst, 0, 0, $w, $h, $c);
            $x = 0;
            $y = 0;
        }
        imagecopyresampled($dst, $new, $x, $y, 0, 0, $w, $h, $width, $height);
        if ($gd_settings['convert_to'] == 'original') {
            $gd_settings['convert_to'] = $ext;
        }
        if (empty($img_functions[$gd_settings['convert_to']])) {
            foreach ($img_functions as $k => $v) {
                if ($v == true) {
                    $gd_settings['convert_to'] = $k;
                    break;
                }
            }
        }
        switch ($gd_settings['convert_to']) {
            case 'gif':
                imagegif($dst, $dest);
                break;
            case 'jpg':
                imagejpeg($dst, $dest, $gd_settings['jpeg_quality']);
                break;
            case 'png':
                imagepng($dst, $dest);
                break;
        }
        @chmod($dest, DEFAULT_FILE_PERMISSIONS);
        return true;
    }
    return false;
}
示例#7
0
文件: func.php 项目: askzap/ultimate
/**
 * @deprecated
 * @since 4.3.1
 */
function fn_create_image_from_file($path, $mime_type)
{
    $ext = fn_get_image_extension($mime_type);
    if ($ext == 'gif') {
        $image = imagecreatefromgif($path);
    } elseif ($ext == 'jpg') {
        $image = imagecreatefromjpeg($path);
    } elseif ($ext == 'png') {
        $image = imagecreatefrompng($path);
    } else {
        return false;
    }
    return $image;
}
示例#8
0
function fn_add_product_options_files($product_data, &$cart, &$auth, $update = false, $location = 'cart')
{
    // Check if products have cusom images
    if (!$update) {
        $uploaded_data = fn_filter_uploaded_data('product_data');
    } else {
        $uploaded_data = fn_filter_uploaded_data('cart_products');
    }
    $dir_path = DIR_CUSTOM_FILES . 'sess_data';
    // Check for the already uploaded files
    if (!empty($product_data['custom_files']['uploaded'])) {
        foreach ($product_data['custom_files']['uploaded'] as $file_id => $file_data) {
            if (file_exists($dir_path . '/' . basename($file_data['path']))) {
                $id = $file_data['product_id'] . $file_data['option_id'] . $file_id;
                $uploaded_data[$id] = array('name' => $file_data['name'], 'path' => $dir_path . '/' . basename($file_data['path']));
                $product_data['custom_files'][$id] = $file_data['product_id'] . '_' . $file_data['option_id'];
            }
        }
    }
    if (!empty($uploaded_data) && !empty($product_data['custom_files'])) {
        $files_data = array();
        foreach ($uploaded_data as $key => $file) {
            $file_info = pathinfo($file['name']);
            $file['extension'] = empty($file_info['extension']) ? '' : $file_info['extension'];
            $file_info = getimagesize($file['path']);
            $file['type'] = $file_info['mime'];
            $file['is_image'] = fn_get_image_extension($file_info['mime']);
            $_data = explode('_', $product_data['custom_files'][$key]);
            $product_id = empty($_data[0]) ? 0 : $_data[0];
            $option_id = empty($_data[1]) ? 0 : $_data[1];
            $file_id = str_replace($option_id . $product_id, '', $key);
            if (empty($file_id)) {
                $files_data[$product_id][$option_id][] = $file;
            } else {
                $files_data[$product_id][$option_id][$file_id] = $file;
            }
        }
        if (!is_dir($dir_path)) {
            if (!fn_mkdir($dir_path)) {
                // Unable to create a directory
                fn_set_notification('E', fn_get_lang_var('error'), str_replace('[directory]', DIR_CUSTOM_FILES, fn_get_lang_var('text_cannot_write_directory')));
            }
        }
    }
    unset($product_data['custom_files']);
    foreach ($product_data as $key => $data) {
        $product_id = !empty($data['product_id']) ? $data['product_id'] : $key;
        // Check if product has cusom images
        if ($update || isset($files_data[$key])) {
            $hash = $key;
        } else {
            $hash = $product_id;
        }
        if (!empty($files_data[$hash]) && is_array($files_data[$hash])) {
            $_options = fn_get_product_options($product_id);
            foreach ($files_data[$hash] as $option_id => $files) {
                foreach ($files as $file_id => $file) {
                    // Check for the allowed extensions
                    if (!empty($_options[$option_id]['allowed_extensions'])) {
                        if (empty($file['extension']) && !empty($_options[$option_id]['allowed_extensions']) || !preg_match("/\\b" . $file['extension'] . "\\b/i", $_options[$option_id]['allowed_extensions'])) {
                            $message = fn_get_lang_var('text_forbidden_uploaded_file_extension');
                            $message = str_replace('[ext]', $file['extension'], $message);
                            $message = str_replace('[exts]', $_options[$option_id]['allowed_extensions'], $message);
                            fn_set_notification('E', fn_get_lang_var('error'), $file['name'] . ': ' . $message);
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    // Check for the max file size
                    if (!empty($_options[$option_id]['max_file_size'])) {
                        if (empty($file['size'])) {
                            $file['size'] = filesize($file['path']);
                        }
                        if ($file['size'] > $_options[$option_id]['max_file_size'] * 1024) {
                            fn_set_notification('E', fn_get_lang_var('error'), str_replace('[size]', $_options[$option_id]['max_file_size'] . ' kb', $file['name'] . ': ' . fn_get_lang_var('text_forbidden_uploaded_file_size')));
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    $_file_path = tempnam($dir_path, 'file_');
                    if (!fn_copy($file['path'], $_file_path)) {
                        fn_set_notification('E', fn_get_lang_var('error'), str_replace('[file]', $file['name'], fn_get_lang_var('text_cannot_create_file')));
                        unset($files_data[$hash][$option_id][$file_id]);
                        continue;
                    }
                    $file['path'] = $_file_path;
                    $file['file'] = basename($file['path']);
                    if ($file['is_image']) {
                        $file['thumbnail'] = 'image.custom_image&image=' . $file['file'] . '&type=T';
                        $file['detailed'] = 'image.custom_image&image=' . $file['file'] . '&type=D';
                    }
                    $file['location'] = $location;
                    if ($update) {
                        $cart['products'][$key]['extra']['custom_files'][$option_id][] = $file;
                    } else {
                        $data['extra']['custom_files'][$option_id][] = $file;
                    }
                }
                if ($update) {
                    if (!empty($cart['products'][$key]['product_options'][$option_id])) {
                        $cart['products'][$key]['product_options'][$option_id] = md5(serialize($cart['products'][$key]['extra']['custom_files'][$option_id]));
                    }
                } else {
                    if (!empty($data['extra']['custom_files'][$option_id])) {
                        $data['product_options'][$option_id] = md5(serialize($data['extra']['custom_files'][$option_id]));
                    }
                }
            }
            // Check the required options
            if (empty($data['extra']['parent'])) {
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && !$update) {
                        if (empty($data['product_options'][$option['option_id']])) {
                            fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('product_cannot_be_added'));
                            unset($product_data[$key]);
                            return array($product_data, $cart);
                        }
                    }
                }
            }
        } else {
            if (empty($data['extra']['parent'])) {
                $_options = fn_get_product_options($product_id);
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && empty($cart['products'][$hash]['extra']['custom_files'][$option['option_id']]) && empty($data['extra']['custom_files'][$option['option_id']])) {
                        fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('product_cannot_be_added'));
                        unset($product_data[$key]);
                        return array($product_data, $cart);
                    }
                }
            }
        }
        if (isset($cart['products'][$key]['extra']['custom_files'])) {
            foreach ($cart['products'][$key]['extra']['custom_files'] as $option_id => $files) {
                foreach ($files as $file) {
                    $data['extra']['custom_files'][$option_id][] = $file;
                }
                $data['product_options'][$option_id] = md5(serialize($files));
            }
        }
        $product_data[$key] = $data;
    }
    return array($product_data, $cart);
}
示例#9
0
function fn_resize_image($src, $new_width = 0, $new_height = 0, $bg_color = '#ffffff')
{
    static $notification_set = false;
    static $gd_settings = array();
    if (file_exists($src) && (!empty($new_width) || !empty($new_height)) && extension_loaded('gd')) {
        $img_functions = array('png' => function_exists('imagepng'), 'jpg' => function_exists('imagejpeg'), 'gif' => function_exists('imagegif'));
        if (empty($gd_settings)) {
            $gd_settings = Settings::instance()->getValues('Thumbnails');
        }
        list($width, $height, $mime_type) = fn_get_image_size($src);
        if (empty($width) || empty($height)) {
            return false;
        }
        $ext = fn_get_image_extension($mime_type);
        if (empty($img_functions[$ext])) {
            if ($notification_set == false) {
                fn_set_notification('E', __('error'), __('error_image_format_not_supported', array('[format]' => $ext)));
                $notification_set = true;
            }
            return false;
        }
        if (empty($new_width) || empty($new_height)) {
            if ($width < $new_width) {
                $new_width = $width;
            }
            if ($height < $new_height) {
                $new_height = $height;
            }
        }
        $dst_width = $new_width;
        $dst_height = $new_height;
        if (empty($new_height)) {
            // if we passed width only, calculate height
            $dst_height = $new_height = $height / $width * $new_width;
        } elseif (empty($new_width)) {
            // if we passed height only, calculate width
            $dst_width = $new_width = $width / $height * $new_height;
        } else {
            // we passed width and height, we need to fit image in this sizes
            if ($new_width * $height / $width > $dst_height) {
                $new_width = $width * $dst_height / $height;
            }
            $new_height = $height / $width * $new_width;
            if ($new_height * $width / $height > $dst_width) {
                $new_height = $height * $dst_width / $width;
            }
            $new_width = $width / $height * $new_height;
            $make_box = true;
        }
        $new_width = intval($new_width);
        $new_height = intval($new_height);
        $dst = imagecreatetruecolor($dst_width, $dst_height);
        if (function_exists('imageantialias')) {
            imageantialias($dst, true);
        }
        if ($ext == 'gif') {
            $new = imagecreatefromgif($src);
        } elseif ($ext == 'jpg') {
            $new = imagecreatefromjpeg($src);
        } elseif ($ext == 'png') {
            $new = imagecreatefrompng($src);
        }
        list($r, $g, $b) = empty($bg_color) ? fn_parse_rgb('#ffffff') : fn_parse_rgb($bg_color);
        $c = imagecolorallocate($dst, $r, $g, $b);
        if (empty($bg_color) && ($ext == 'png' || $ext == 'gif')) {
            if (function_exists('imagecolorallocatealpha') && function_exists('imagecolortransparent') && function_exists('imagesavealpha') && function_exists('imagealphablending')) {
                $c = imagecolorallocatealpha($dst, 255, 255, 255, 127);
                imagecolortransparent($dst, $c);
                imagesavealpha($dst, true);
                imagealphablending($dst, false);
            }
        }
        imagefilledrectangle($dst, 0, 0, $dst_width, $dst_height, $c);
        if (!empty($make_box)) {
            $x = intval(($dst_width - $new_width) / 2);
            $y = intval(($dst_height - $new_height) / 2);
        } else {
            $x = 0;
            $y = 0;
        }
        imagecopyresampled($dst, $new, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
        // Free memory from image
        imagedestroy($new);
        if ($gd_settings['convert_to'] == 'original') {
            $convert_to = $ext;
        } elseif (!empty($img_functions[$gd_settings['convert_to']])) {
            $convert_to = $gd_settings['convert_to'];
        } else {
            $convert_to = key($img_functions);
        }
        ob_start();
        if ($convert_to == 'gif') {
            imagegif($dst);
        } elseif ($convert_to == 'jpg') {
            imagejpeg($dst, null, $gd_settings['jpeg_quality']);
        } elseif ($convert_to == 'png') {
            imagepng($dst);
        }
        $content = ob_get_clean();
        return array($content, $convert_to);
    }
    return false;
}
示例#10
0
 private static final function getApiImageBinData($icon, $params, $type = 'product')
 {
     if (!empty($icon['absolute_path'])) {
         $image_file = $icon['absolute_path'];
     } else {
         $_image_file = db_get_field("SELECT image_path FROM ?:images WHERE image_id = ?i", $params['image_id']);
         $image_file = Registry::get('config.dir.images') . $type . '/' . $_image_file;
     }
     if (extension_loaded('gd') && (!empty($params['image_x']) || !empty($params['image_y']))) {
         $new_image_x = !empty($params['image_x']) ? $params['image_x'] : $params['image_y'] / $icon['image_y'] * $icon['image_x'];
         $new_image_y = !empty($params['image_y']) ? $params['image_y'] : $params['image_x'] / $icon['image_x'] * $icon['image_y'];
         $new_image_gd = imagecreatetruecolor($new_image_x, $new_image_y);
         list(, , $mime_type) = fn_get_image_size($image_file);
         $ext = fn_get_image_extension($mime_type);
         if ($ext == 'gif' && function_exists('imagegif')) {
             $image_gd = imagecreatefromgif($image_file);
         } elseif ($ext == 'jpg' && function_exists('imagejpeg')) {
             $image_gd = imagecreatefromjpeg($image_file);
         } elseif ($ext == 'png' && function_exists('imagepng')) {
             $image_gd = imagecreatefrompng($image_file);
         } else {
             return false;
         }
         imagecopyresized($new_image_gd, $image_gd, 0, 0, 0, 0, $new_image_x, $new_image_y, $icon['image_x'], $icon['image_y']);
         $tmp_file = fn_create_temp_file();
         if ($ext == 'gif') {
             imagegif($new_image_gd, $tmp_file);
         } elseif ($ext == 'jpg') {
             imagejpeg($new_image_gd, $tmp_file, 50);
         } elseif ($ext == 'png') {
             imagepng($new_image_gd, $tmp_file, 0);
         }
         if (!($image_data = fn_get_contents($tmp_file))) {
             return false;
         }
         $icon['data'] = base64_encode($image_data);
         $icon['image_x'] = $new_image_x;
         $icon['image_y'] = $new_image_y;
     } elseif (fn_get_contents($image_file)) {
         $image_data = fn_get_contents($image_file);
         $icon['data'] = base64_encode($image_data);
     }
     return $icon;
 }
示例#11
0
/**
 * Resizes image
 * @param string $src source image path
 * @param integer $new_width new image width
 * @param integer $new_height new image height
 * @param string $bg_color new image background color
 * @param array $custom_settings custom convertion settings
 * @return array - new image contents and format
 */
function fn_resize_image($src, $new_width = 0, $new_height = 0, $bg_color = '#ffffff', $custom_settings = array())
{
    static $general_settings = array();
    if (empty($general_settings)) {
        $general_settings = Settings::instance()->getValues('Thumbnails');
    }
    gc_collect_cycles();
    $settings = empty($custom_settings) ? $general_settings : $custom_settings;
    /** @var \Imagine\Image\ImagineInterface $imagine */
    $imagine = Tygh::$app['image'];
    if (!empty($bg_color) && !preg_match('/^#([0-9a-f]{3}){1,2}$/i', $bg_color)) {
        $bg_color = '#ffffff';
    }
    try {
        $image = $imagine->open($src);
        $image->usePalette(new \Imagine\Image\Palette\RGB());
        $filter = $imagine instanceof \Imagine\Gd\Imagine ? \Imagine\Image\ImageInterface::FILTER_UNDEFINED : \Imagine\Image\ImageInterface::FILTER_LANCZOS;
        $new_size = new \Imagine\Image\Box($new_width, $new_height);
        $thumbnail = $image->thumbnail($new_size, \Imagine\Image\ImageInterface::THUMBNAIL_INSET, $filter);
        // Created thumbnail is smaller than required size, so we create
        // an empty canvas of required size and center thumbnail on it
        if (!$thumbnail->getSize()->contains($new_size)) {
            $thumbnail_coordinates = new \Imagine\Image\Point((int) (($new_size->getWidth() - $thumbnail->getSize()->getWidth()) / 2), (int) (($new_size->getHeight() - $thumbnail->getSize()->getHeight()) / 2));
            $canvas_color = empty($bg_color) ? $image->palette()->color('#FFF', 0) : $image->palette()->color($bg_color);
            $canvas = $imagine->create($new_size, $canvas_color);
            $canvas->paste($thumbnail, $thumbnail_coordinates);
            unset($thumbnail);
            $thumbnail = $canvas;
        }
        unset($image);
        $format = $settings['convert_to'];
        if ($format === 'original') {
            if ($original_file_type = fn_get_image_extension(fn_get_mime_content_type($src, false))) {
                $format = $original_file_type;
            } else {
                $format = 'png';
            }
        }
        $options = array('jpeg_quality' => $settings['jpeg_quality'], 'png_compression_level' => 9, 'filter' => $filter);
        $return = array($thumbnail->get($format, $options), $format);
        unset($thumbnail);
        gc_collect_cycles();
        return $return;
    } catch (\Exception $e) {
        gc_collect_cycles();
        return false;
    }
}