Skip to content

Commit

Permalink
Merge pull request #43 from nguyenanhung/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
hungnguyenhp authored Sep 15, 2023
2 parents 54b7ce1 + 9ccc355 commit fd02ebe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 231 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ext-mbstring": "*",
"nguyenanhung/html-helper": "^2.0 || ^1.0",
"nguyenanhung/mobile-helper": "^2.0 || ^1.0",
"nguyenanhung/image-helper": "^2.0 || ^1.0",
"nguyenanhung/basic-miscellaneous-helper": "^2.0 || ^1.0"
},
"require-dev": {
Expand Down
42 changes: 8 additions & 34 deletions helpers/image_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
/**
* Function google_image_resize
*
* @param string $url
* @param int|null $width
* @param int|null $height
* @param string $url
* @param int|null $width
* @param int|null $height
* @param string|null $server
*
* @return string
Expand Down Expand Up @@ -86,33 +86,7 @@ function wordpress_proxy_dns_prefetch()
*/
function bear_framework_image_url($input = '', $server = '', $base = 'live')
{
$images_url = trim($input);
$images_url = str_replace('http://cdnphoto.dantri.com.vn/', 'https://cdnphoto.dantri.com.vn/', $images_url);
if (function_exists('config_item') && !empty($images_url)) {
$no_thumb = array(
'images/system/no_avatar.jpg',
'images/system/no_avatar_100x100.jpg',
'images/system/no_video_available.jpg',
'images/system/no_video_available_thumb.jpg',
'images/system/no-image-available.jpg',
'images/system/no-image-available_60.jpg',
'images/system/no-image-available_330.jpg'
);
if (in_array($images_url, $no_thumb)) {
return assets_url($images_url);
}
$parse = parse_url($images_url);
if (isset($parse['host'])) {
return $images_url;
}
if (trim(mb_substr($images_url, 0, 12)) === 'crawler-news') {
$images_url = trim('uploads/' . $images_url);
}
$images_url = str_replace(array('upload-vcms/news/news/', 'upload-vcms/mheath/mheath/'), array('upload-vcms/news/', 'upload-vcms/mheath/'), $images_url);
return config_item('static_url') . $images_url;
}

return $images_url;
return nguyenanhung\CodeIgniter\BasicHelper\ImageHelper::formatImageUrl($input, $server, $base);
}
}
if (!function_exists('bear_framework_create_image_thumbnail')) {
Expand All @@ -123,10 +97,10 @@ function bear_framework_image_url($input = '', $server = '', $base = 'live')
* @param $width
* @param $height
*
* @return mixed|string|null
* @return string|null
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 25/02/2023 27:17
* @time : 15/09/2023 57:40
*/
function bear_framework_create_image_thumbnail($url = '', $width = 100, $height = 100)
{
Expand All @@ -141,10 +115,10 @@ function bear_framework_create_image_thumbnail($url = '', $width = 100, $height
* @param $width
* @param $height
*
* @return mixed|string|null
* @return string|null
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 15/06/2022 03:06
* @time : 15/09/2023 50:33
*/
function create_image_thumbnail($url = '', $width = 100, $height = 100)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class BaseHelper
{
const VERSION = '1.5.7';
const VERSION = '1.5.8';
const LAST_MODIFIED = '2023-09-15';
const PROJECT_NAME = 'CodeIgniter - Basic Helper';
const AUTHOR_NAME = 'Hung Nguyen';
Expand Down
209 changes: 13 additions & 196 deletions src/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace nguyenanhung\CodeIgniter\BasicHelper;

use Exception;
use nguyenanhung\Libraries\ImageHelper\ImageHelper as ImageHelperLib;

/**
* Class ImageHelper
Expand All @@ -21,231 +21,48 @@
*/
class ImageHelper extends BaseHelper
{
/**
* Function googleGadgetsProxyServer
*
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* @return string[]|array
*/
public static function googleGadgetsProxyServerList()
{
return array('images1', 'images2', 'images3', 'images4', 'images5', 'images6', 'images7', 'images8', 'images9', 'images10');
return ImageHelperLib::googleGadgetsProxyServerList();
}

/**
* Function wordpressProxyProxyServerList
*
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* @return string[]|array
*/
public static function wordpressProxyProxyServerList()
{
return array('i0', 'i1', 'i2', 'i3');
return ImageHelperLib::wordpressProxyProxyServerList();
}

/**
* Function googleGadgetsProxy
*
* @param string $url
* @param int|null $width
* @param int|null $height
* @param string|null $server
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 08/20/2021 11:20
*/
public static function googleGadgetsProxy($url = '', $width = 100, $height = null, $server = 'images1')
{
$server = trim($server);
if (in_array($server, self::googleGadgetsProxyServerList())) {
$proxyUrl = 'https://' . trim($server) . '-focus-opensocial.googleusercontent.com/gadgets/proxy';
} else {
$proxyUrl = 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy';
}
$proxyContainer = 'focus';
$proxyRefresh = 2592000;
// Start
$params = array();
$params['url'] = $url;
if ($width !== null) {
$params['resize_w'] = $width;
}
if ($height !== null) {
$params['resize_h'] = $height;
}
$params['container'] = $proxyContainer;
$params['refresh'] = $proxyRefresh;
// Result URL
$url = $proxyUrl . '?' . urldecode(http_build_query($params));
return trim($url);
return ImageHelperLib::googleGadgetsProxy($url, $width, $height, $server);
}

/**
* Function googleGadgetsProxyDnsPrefetch
*
* @param $server
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* @return string
*/
public static function googleGadgetsProxyDnsPrefetch($server = 'images1')
{
$html = '';
if ($server === 'full') {
foreach (self::googleGadgetsProxyServerList() as $proxy) {
$html .= "<link href='//" . trim($proxy) . "-focus-opensocial.googleusercontent.com' rel='dns-prefetch' />" . PHP_EOL;
}
} else {
$html .= "<link href='//images1-focus-opensocial.googleusercontent.com' rel='dns-prefetch' />" . PHP_EOL;
}
return $html;
return ImageHelperLib::googleGadgetsProxyDnsPrefetch($server);
}

/**
* Function wordpressProxy
*
* @param string $imageUrl
* @param string $server
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 08/20/2021 11:39
*/
public static function wordpressProxy($imageUrl = '', $server = 'i3')
{
$url = parse_url($imageUrl);
$schema = isset($url['scheme']) ? $url['scheme'] : '';
$host = isset($url['host']) ? $url['host'] : '';
if (empty($host)) {
return trim($imageUrl);
}
if ($schema === 'http') {
// Default, WordPress Proxy not Support HTTP Protocol -> Auto Switch Google GadgetsProxy
return self::googleGadgetsProxy($imageUrl, null);
}
$protocol = array($schema . '://', '//',);
$imageUrl = str_replace($protocol, '', $imageUrl);
$server = trim($server);
if (in_array($server, self::wordpressProxyProxyServerList())) {
$proxyUrl = 'https://' . trim($server) . '.wp.com/';
} else {
$proxyUrl = 'https://i3.wp.com/';
}
$url = $proxyUrl . $imageUrl;
return trim($url);
return ImageHelperLib::wordpressProxy($imageUrl, $server);
}

/**
* Function wordpressProxyDnsPrefetch
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 09/15/2021 33:45
*/
public static function wordpressProxyDnsPrefetch()
{
$html = '';
foreach (self::wordpressProxyProxyServerList() as $server) {
$html .= "<link href='//" . trim($server) . ".wp.com' rel='dns-prefetch' />" . PHP_EOL;
}
return $html;
return ImageHelperLib::wordpressProxyDnsPrefetch();
}

/**
* Function createThumbnail - Only use for CodeIgniter
*
* @param $url
* @param $width
* @param $height
*
* @return mixed|string|null
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 15/06/2022 00:20
*/
public static function createThumbnail($url = '', $width = 100, $height = 100)
{
try {
if (function_exists('base_url') && function_exists('config_item') && class_exists('nguyenanhung\MyImage\ImageCache')) {
$tmpPath = config_item('image_tmp_path');
$storagePath = config_item('base_storage_path');
$cache = new \nguyenanhung\MyImage\ImageCache();
$cache->setTmpPath($tmpPath)->setUrlPath(base_url($storagePath))->setDefaultImage();
$thumbnail = $cache->thumbnail($url, $width, $height);
if (!empty($thumbnail)) {
return $thumbnail;
}

return $cache->thumbnail(config_item('image_path_tmp_default'), $width, $height);
}

return $url;
} catch (Exception $e) {
if (function_exists('log_message')) {
log_message('error', __get_error_message__($e));
}

return $url;
}
return ImageHelperLib::createThumbnail($url, $width, $height);
}

/**
* Function createThumbnailWithCodeIgniterCache
*
* @param $url
* @param $width
* @param $height
*
* @return mixed|string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 25/02/2023 27:41
*/
public static function createThumbnailWithCodeIgniterCache($url = '', $width = 100, $height = 100)
{
try {
if (function_exists('base_url') && function_exists('config_item')) {
$cacheKey = md5('createThumbnailWithCodeIgniterCache' . $url . $width . $height);
$cacheTtl = 15552000; // Cache 6 tháng
// Setup CodeIgniter
$CI =& get_instance();
$CI->load->driver('cache', array('adapter' => 'file', 'backup' => 'dummy'));
if (!$urlThumbnail = $CI->cache->get($cacheKey)) {
$tmpPath = config_item('image_tmp_path');
$storagePath = config_item('base_storage_path');
$imageCache = new \nguyenanhung\MyImage\ImageCache();
$imageCache->setTmpPath($tmpPath)->setUrlPath(base_url($storagePath))->setDefaultImage();
$thumbnail = $imageCache->thumbnail($url, $width, $height);
if (!empty($thumbnail)) {
$urlThumbnail = $thumbnail;
} else {
$thumbnailTmp = $imageCache->thumbnail(config_item('image_path_tmp_default'), $width, $height);
$urlThumbnail = $thumbnailTmp;
}
if ($urlThumbnail !== null) {
$CI->cache->save($cacheKey, $urlThumbnail, $cacheTtl);
}
}
if (!empty($urlThumbnail)) {
return $urlThumbnail;
}

return $url;
}

return $url;
} catch (Exception $e) {
if (function_exists('log_message')) {
log_message('error', __get_error_message__($e));
}
return ImageHelperLib::createThumbnailWithCodeIgniterCache($url, $width, $height);
}

return $url;
}
public static function formatImageUrl($input = '', $server = '', $base = 'live')
{
return ImageHelperLib::formatImageUrl($input, $server, $base);
}
}

0 comments on commit fd02ebe

Please sign in to comment.