-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Image_class #279
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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} | ||
* @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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
* | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it's ok to have a separate 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to avoid the word |
||
{ | ||
$this->imageFolder = $folder; | ||
return $this; | ||
} | ||
|
||
// ------------------------------------------------------------------ | ||
|
||
/** | ||
* @param string $filename | ||
* @return resource | ||
*/ | ||
public function setFilename(string $filename): resource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
{ | ||
$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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming of this method is a little awkward, Perhaps it would be more appropriately named |
||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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.