Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active April 8, 2023 03:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosmomathieu/6c551f1cef0cba095398071801bde921 to your computer and use it in GitHub Desktop.
Save cosmomathieu/6c551f1cef0cba095398071801bde921 to your computer and use it in GitHub Desktop.
<?php
$file = $file['temp']['file'];
$image = new Image($file);
$image->save(${filename}, [optional]${quality}); // Use this when moving a file from one location to another. Especially in the case when we upload a file to the server.
$image->resize(${width}, ${height}, [optional]${crop}, [optional]${replace});
$image->createThumb(${width}, ${height}, [optional] ${save_path});
$image->getWidth();
$image->getHeight();
$image->crop()->save(); // Maybe?
// Recursively create thumbnails based on config array
$thumbConfig = array(
[
'width' => 0,
'height' => 0,
'filename' => 0,
]
));
foreach($thumbConfig as $configItem) {
list($filename, $width, $height) = $configItem;
$image->createThumb($width, $height, $filename);
}
namespace Libraries;
/**
* Image handling and manipulation library
*
* This library makes use of the CodeIgniter image manipulation library.
* @author Cosmo Mathieu <cosmo@cosmointeractive.co>
*/
class Image
{
protected $adapter;
protected $originalImage = '';
protected $originalWidth = 0;
protected $originalHeight = 0;
protected $newImage = '';
protected $width = 0;
protected $height = 0;
protected $type = '';
protected $attr;
/**
* Setup naming policy for the image
*
* When to true an incremental number is appended to the end of the filename.
* When false, images of the same filename are replaced.
* @var bool
*/
protected $allowDuplicates = true;
public function __construct($image)
{
$this->adapter = ci()->load->library('image_lib');
$this->originalImage = $image;
list($width, $height, $type, $attr) = getimagesize($image);
$this->originalWidth = $width;
$this->originalHeight = $height;
$this->type = $type;
$this->attr = $attr;
};
protected function readinessCheck()
{
// Check for GD
// Check if image exists
// Check if save path is_writable
}
protected function _getProperties()
{
$this->newImage = $this->originalImage;
}
/**
* Create a new version of the file, resize, and save to a new location
*
* @param int $width [description]
* @param int $height [description]
* @param bool $replace Replace the image if found, or keep both
* @return [type] [description]
*/
public function resize($width, $height, $replace = true)
{
// $this->adapter->resize($width, $height);
return $this->_resize($width, $height, $replace);
}
public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
{
$this->crop($top = 0, $left = 0, $right = 0, $bottom = 0);
return $this;
}
/**
* Save the provided image to a location specified
*
* @param string $path The image path including filename and extension
* @param int $quality The image quality
* @return bool
*/
public function save($path = null, $quality = null)
{
// If path is empty or null, check if original filename is set and use that
if (is_null($path) || empty($path)) {
$path = $this->originalImage;
}
// TODO: Add remaining logic
}
protected function _crop($top = 0, $left = 0, $right = 0, $bottom = 0)
{
if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0) {
return false;
}
// Determine dimensions from coordinates
$newWidth = $this->originalWidth - $left - $right;
$newHeight = $this->originalHeight - $top - $bottom;
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->originalImage;
$config['new_image'] = $this->originalImage;
$config['create_thumb'] = true;
$config['maintain_ratio'] = false;
$config['width'] = $newWidth;
$config['height'] = $newHeight;
ci()->load->library('image_lib');
ci()->image_lib->clear();
ci()->image_lib->initialize($config);
ci()->image_lib->resize();
ci()->image_lib->clear();
// TODO: Add verification method
}
protected function _resize($width, $height, $replace = true, $keepAspectRatio = true)
{
//The original sizes
$original_width = $this->originalWidth;
$original_height = $this->originalHeight;
$ratio = $original_width / $original_height;
//The requested sizes
$requested_width = $width;
$requested_height = $height;
// Keep current dimensions of the requested image if both less than the current
if ($original_width < $requested_width && $original_height < $requested_height) {
$requested_width = $original_width;
$requested_height = $original_height;
}
if ($requested_width == 0) {
$requested_width = $original_width;
}
if ($requested_height == 0) {
$requested_height = $original_height;
}
// Resize Image
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->originalImage;
$config['new_image'] = $this->newImage;
$config['maintain_ratio'] = $keepAspectRatio;
$config['height'] = $requested_height;
$config['width'] = $requested_width;
ci()->load->library('image_lib');
ci()->image_lib->clear();
ci()->image_lib->initialize($config);
ci()->image_lib->resize();
ci()->image_lib->clear();
// TODO: Add verification method
}
protected function logError($error, $severity = 0)
{
$this->errorMessages[] = $error;
log_error($error, $severity);
}
// ---------------------------------------------------------------
// Getters
// ---------------------------------------------------------------
public function errorMessage()
{
return $this->errorMessages;
}
}
namespace Libraries\Image\Adapter;
class AdapterInterface
{
public function checkDependencies();
protected function readinessCheck();
}
class AbstractAdapter
{
/**
* Required extensions
*
* @var array
*/
protected $requiredExtensions = ["gd"];
protected function readinessCheck()
{
// Check for GD
}
public function resize();
}
namespace Libraries\Image\Adapter;
class Gd extends AbstractAdapter implements AdapterInterface;
{
}
namespace Libraries\Image\Adapter;
class CodeIgniter extends AbstractAdapter implements AdapterInterface
{
}
// Make this into a collection?
$gallery = new MediaGallery(${id});
$images = $gallery->getMediaGalleryImages();
$image = $images->getFirstItem();
{{ builder:gallery_block_html type="grid" id="${int}" filter="all|public|hidden" sort="asc|desc" }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment