-
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 #114
Comments
Kudos firstly for this, but If it would be possible, I want to add one more feature that would be awesome: Advantage in practice: User uploads his profile picture and it gets cropped automatically perfectly. |
I agree that will be a great addition. I will look into what all it would entail, because it's definitely something I'd love to see be part of it, but I'd have to be able to support it across all supported graphics libraries (GD, ImageMagick, GraphicsMagick?) |
GraphicsMagick would be great, imagemagick is slow and for providers is the new fork way better |
Recommend that there is a REAL solid game plan with how image manipulation is instantiated and interacted with, with emphasis on:
|
Excuse my ignorance... Why can not use for CI4 a third party library for example this: This library provide all solutions to complete this task and support imagemagick and GD. We can create a image class that use this library? Maybe it's crazy....??? |
@davidgv88 I hadn't seen that library before. And it looks great! But, keeping with a long history of CI being primarily a self-contained framework, we're not pulling in outside libs. The last time I recommended that, didn't go well. :) That does look like an excellent library to study, though! |
To play devil's advocate: why maintain an image library at all, then? Image manipulation is not a core foundation of the framework (such as a router or ORM might be). It is not integrated closely with other parts of the framework, such as the request process. If good user-land packages already exist, what benefits are you providing the community by creating another? |
And playing the normal Devil's Advocate to your Devil's Advocate:
Good user-land packages exist for every part of the framework. The often have different philosophies, different ways of working, etc, but they exist and can be wired together. Heck, that's what 1/2 of Laravel is. I'm not saying this just because I think we should do the whole, "We didn't make it" thing, because, hell, I'm the one who has to build most of it, and this is a long process. :) I'll be happy to bring it up, because it's not a core part of the framework. It is a part that CI has always had so I think many people see it as belonging in the framework. And I get what you're saying, and ask myself similar things fairly frequently these days. But, in the end, I'm not the owner of the framework. In many ways the community that's been using it for the last decade (myself included) are the "owners". I'm just the steward tasked with the job of creating a more modern, hopefully better version. In the process, we're porting most of the existing functionality from the old framework into the new, and that's all this library was meant to do: provide existing functionality with some new tweaks that are useful in how today's workflows and needs are. And it might be a little integrated into the Uploader library, though any image library we included could be used... |
i think it's important, that CI has its own library, the less external sources the better and to be honest, for all of us it's better to maintain one framework and not always have to update 3rd party extensions, every additional extension is a risk to break something, to add new bugs while updating and much more. |
I was playing a bit with the CI 3.x Image_Lib class to extend it functionality for thumbnail creation and noticed two features that I am missing right now. What I was looking for is a method to create a thumbnail of given size (as set in the configuration) while retaining the original image_ratio: As you see, I have filled the top/bottom (and for other images the side) with a blurred version of the original image to make the image fit the required dimensions. With the original CI 3.x library this is not possible as only one of two dimensions set in the configuration is altually retained if the configuration item "maintain_ratio" is set to true. I have now written a customized version of the Image_Lib library to use within my application. Maybe it can be considered to put similar functionality in the new library if not done so already: Chaining modifications Gaussian blur /**
* Returns the given image with gaussian blur applied
*
* @param $src_img The source image to process (GD2 resource)
* @param $strengh The strength of the blur to apply
* @return Object The resulting image
*/
function blur_gd($src_img, $strength = 3)
{
if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
{
$create = 'imagecreatetruecolor';
$copy = 'imagecopyresampled';
}
else
{
$create = 'imagecreate';
$copy = 'imagecopyresized';
}
// Determine target dimensions
$originalWidth = imagesx($src_img);
$originalHeight = imagesy($src_img);
$smallestWidth = ceil($originalWidth * 0.5 ** $strength);
$smallestHeight = ceil($originalHeight * 0.5 ** $strength);
// Prepare first iteration
$prevImage = $src_img;
$prevWidth = $originalWidth;
$prevHeight = $originalHeight;
for($i = 0; $i < $strength; $i += 1)
{
// Determine new dimensions
$nextWidth = $smallestWidth * 2 ** $i;
$nextHeight = $smallestHeight * 2 ** $i;
// Scale down & blur image
$nextImage = $create($nextWidth, $nextHeight);
$copy($nextImage, $prevImage, 0, 0, 0, 0, $nextWidth, $nextHeight, $prevWidth, $prevHeight);
imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);
// Move on...
$prevImage = $nextImage;
$prevWidth = $nextWidth;
$prevHeight = $nextHeight;
}
// Scale back & blur image
$copy($src_img, $nextImage, 0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
imagefilter($src_img, IMG_FILTER_GAUSSIAN_BLUR);
imagedestroy($prevImage);
return $src_img;
} |
Hi @Xirt In all my projects I never use the CI Image_Lib. It´s very basic. I personally always use "Image Intervention" library. In the blur case, you can use the library that I mentioned before, the example code could be: $manager = new \Intervention\Image\ImageManager();
$background = $manager->canvas(500, 500);
$img_blur = $manager->make('example.jpeg')->fit(500, 500)->blur(100);
$background->insert($img_blur);
$image = $manager->make('example.jpeg')->resize(500, 500, function ($c) {
$c->aspectRatio();
$c->upsize();
});
// insert resized image centered into background
$background->insert($image, 'center');
// save or do whatever you like
$background->save('example_resized.jpg'); |
@AntonyGarand Thanks for the comments. Chaining is already being baked in, so you're good there, assuming that nothing comes up to make me rewrite that. While that's a pretty cool way to handle the thumbnails, it's definitely a stylized way to handle it, so probably not one that I'll provide out of the box. We want to leave it flexible for everyone to match their own style. I'm undecided about features like blur. Once we start adding features like that we really need to keep going and add features like changing brightness, grayscale, etc And, while I'd love to see that out of the box, as @Portaflex said, ImageIntervention is a great library. And I'm just one man doing this and can't really afford to spend a couple of months (or more) making all of these features work for all supported drivers (gd, ImageMagick, GraphicsMagick, and NetPBM). |
Alternatively I could think of having an option to retrieve the Image Resource from the class so that it can be used in other (external or self-written) libraries if required? This way people can use the functionalities available within the CI library, but they can program themselves any functionalities they are missing (or use a different library for that). I could not find an 'official' way to perform this task with CI 3.x, but as you indicated chaining is now possible I can imagine the Image Resource is available internally hence this should now become a possibility. |
@Xirt yeah, that should not be a problem at all. And sounds like a great addition. I can add a getter for it. I have a feeling for any extensions, though, you'll probably just extend the correct Handler, which would give you access to the protected class var, anyway. |
This class represents a single image file and provides many features to work directly with the image itself, whether GD, GD2, or ImageMagic is present, through Handlers. Provides many common solutions to tasks like:
Development Checklist:
The text was updated successfully, but these errors were encountered: