Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image_class #279

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions system/Image/GD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php namespace CodeIgniter\Image;

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2016, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link http://codeigniter.com
* @since Version 3.0.0
* @filesource
*/

/**
* Image manipulation GD API.
*
* @package CodeIgniter\Image
*/
class GD extends Image implements ImageInterface
{
/**
* Function to resize image.
*
* @param resource $image
* @param int $new_width
* @param int $new_height
* @return resource
*/
public function resize(int $new_width, int $new_height): resource
{
imagescale($this->resource, $new_width, $new_height);

return $this->resource;
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take the time to write full docblocks both here and in the interface. The @inheritdoc tag works great for machines, but not for humans, unfortunately.

* @see \CodeIgniter\Image\ImageInterface::crop()
*/
public function crop(int $x, int $y, int $new_width, int $new_height): resource
{
imagecrop($this->resource, ['x' => $x, 'y' => $y,
'width' => $new_width, 'height' => $new_height]);

return $this->resource;
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::rotate()
*/
public function rotate(int $angle, string $bg_color = 'fff', string $transparency = 0): resource
{
imagerotate($this->resource, $angle, $bgd_color, $transparency);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we should be providing some form of validation of the validity of the parameters here to ensure they're within valid ranges and provide decent error messages, unless the error messages we get back from failures of that command are already good?


return $this->resource;
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::flip()
*/
public function flip(string $mode): resource
{
imageflip($this->resource, $mode);

return $this->resource;
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::thumbnail()
*/
public function thumbnail(int $thumbWidth): resource
{
$width = imagesx($this->resource);
$height = imagesy($this->resource);

$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

$thumb = imagecreatetruecolor($new_width, $new_height);

imagecopyresized($thumb, $this->resource, 0, 0, 0, 0, $new_width, $new_height,
$width, $height);

return $thumb;
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::watermark()
*/
public function watermark(string $text, int $width = 100, int $height = 50, int $font = 5): resource
{
$width = imagesx($this->resource) * ($width / 100);
$height = imagesy($this->resource) * ($height / 100);

$stamp = imagecreate($width, $height);

imagefilltoborder($stamp, 0, 0, imagecolorallocate($this->resource, 0, 0, 0),
imagecolorallocate($this->resource, 010, 010, 010));

$x = $width / 10;
$y = $height / 10;
$color = imagecolorallocate($stamp, 0, 0, 255);
imagestring($stamp, $font, $x, $y, $text, $color);

imagecopymerge($this->resource, $stamp, 0, 0, 0, 0, $width, $height, 50);

return $this->resource;

}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::convert()
*/
public function convert(string $final_name, string $format): resource
{
switch ($format)
{
case 'jpg':
imagejpeg($this->resoruce, $final_name);
case 'png':
imagepng($this->resource, $final_name);
}
}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::restoreOrientation()
*/
public function resetOrientation()
{

}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::compress()
*/
public function compress()
{

}

// ------------------------------------------------------------------

/**
* {@inheritDoc}
* @see \CodeIgniter\Image\ImageInterface::copy()
*/
public function copy()
{

}
}
1 change: 1 addition & 0 deletions system/Image/Gmagick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
163 changes: 163 additions & 0 deletions system/Image/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php namespace CodeIgniter\Image;

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2016, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link http://codeigniter.com
* @since Version 3.0.0
* @filesource
*/

/**
* Image manipulation factory.
*
* Creates and returns an instance of the appropriate image manipulation processor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this should be more than just a simple factory class. While it can definitely serve as that, with the image()/fromFile() method below, this class also would allow the perfect place to put some bulk methods. Unsure of the exact details of those at the moment, it would be very handy to be able to specify things like:

  • multiple sizes to create of a single image
  • an action like resize, createThumb, or correctOrientation, that could be performed on every image within a specified directory.

*
* @package CodeIgniter\Image
*/
class Image
{
protected $driver;
protected $resource;
protected $imageFullPath;
protected $imageFolder;
protected $imageFilename;
protected $imageExtension;
protected $mime;

protected $imageTypes = [
'jpg',
'jpeg',
'png',
'gif',
'tiff'
];

// ------------------------------------------------------------------

/**
* @param string $driver
* @return resource
*/
public function load(string $driver): resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's ok to have a separate load() method, we should have it load the correct value directly, based upon a configuration setting or two, which would be passed in via the constructor. Ideally this should be it's own config file, application/Config/Images.php.

Then, a new entry in Config\Services would make that simple to load.

{
$classname = "Codeigniter\\Image\\$driver";
return new $classname;
}

// ------------------------------------------------------------------

/**
* @param string $folder
* @return resource
*/
public function setFolder(string $folder): resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid the word folder in favor of directory. Though, destination might be more appropriate here.

{
$this->imageFolder = $folder;
return $this;
}

// ------------------------------------------------------------------

/**
* @param string $filename
* @return resource
*/
public function setFilename(string $filename): resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setFolder(), setFilename(), and similar methods seem like they would be better attached to the different Handlers as ways to set new attributes on the files. Actually, that doesn't make sense, either, because you'd pass that information directly to the copy() command. Since the image() method below already takes a full path, I'd say get rid of these functions. This also frees this class up to be a little more useful than just a factory. I'll make a note of that elsewhere.

{
$this->imageFilename = $filename;
return $this;
}

// ------------------------------------------------------------------

/**
* @param string $extension
* @throws \InvalidArgumentException
* @return resource
*/
public function setExtension(string $extension): resource
{
if (in_array($extension, $this->imageTypes))
{
$this->imageExtension = strtolower($extension);
return $this;
}
else
{
throw new \InvalidArgumentException('Image extension is not allowed');
}
}

// ------------------------------------------------------------------

/**
* @param string $full_image_path
* @throws \InvalidArgumentException
* @return resource
*/
public function image(string $full_image_path = false, $driver): resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this method is a little awkward, Image::image(), and doesn't really specify whether we're creating a new empty image to work with, reading one from file, etc. And your docblock doesn't help out there either. :)

Perhaps it would be more appropriately named fromFile()?

{
if ($full_image_path)
{
$route = pathinfo($full_image_path);
$this->imageFilename = $route['filename'];
$this->imageFolder = $route['dirname'];
$extension = $rute['extension'];

if (in_array($extension, $this->imageTypes))
{
$this->imageExtension = strtolower($extension);
}
else
{
throw new \InvalidArgumentException('Image extension is not allowed');
}
}

$this->imageFullPath = BASEPATH.$this->imageFolder.'/'.$this->imageFilename.'.'.
$this->imageExtension;

$this->mime = "image/$this->imageExtension";

if(file_exists($this->imageFullPath))
{
header('content_type :'.$this->mime);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting a header here? That only matters if we're sending the file to the browser. If we're only modifying a local file, this is pointless and could lead to inaccurate headers being sent. Additionally, it has issues with content/header order errors, and, if it was desired, should be using the Response object for consistency and safety.

$this->resource = readfile($this->imageFullPath);
}
else
{
throw new \InvalidArgumentException('No image found in specified location');
}

return $this->load($driver);
}
}
Loading