Navigation Menu

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 #114

Closed
8 tasks
lonnieezell opened this issue Jun 15, 2016 · 15 comments
Closed
8 tasks

Image Class #114

lonnieezell opened this issue Jun 15, 2016 · 15 comments
Assignees
Labels
new feature PRs for new features
Milestone

Comments

@lonnieezell
Copy link
Member

lonnieezell commented Jun 15, 2016

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:

  • Resize (with centering options)
  • Crop (with centering options)
  • Rotate
  • create thumbnail (with renaming options, like adding size to filename)
  • work with EXIF data
  • Watermark text or black and white image onto
  • Convert between common formats (?)
  • Reset orientation (see https://rtmedia.io/docs/troubleshooting/fixing-image-orientation-wordpress-uploads/ for brief discussion/solution) Ensure all rotations are covered.
  • Compress
  • Copy

Development Checklist:

  • Component(s)
  • ... with PHPdocs
  • Unit testing
  • ... with >80% coverage
  • User guide updated
  • Classmap integration?
  • Securely signed commits
  • Conforms to style guide
@lonnieezell lonnieezell added the new feature PRs for new features label Jun 15, 2016
@lonnieezell lonnieezell added this to the Pre-Alpha 2 milestone Jun 15, 2016
@lonnieezell lonnieezell mentioned this issue Jun 17, 2016
8 tasks
@CodeBrauer
Copy link

Kudos firstly for this, but If it would be possible, I want to add one more feature that would be awesome:
Cropping Images in PHP Based on their Entropy
Related: https://github.com/stojg/crop

Advantage in practice: User uploads his profile picture and it gets cropped automatically perfectly.

@lonnieezell
Copy link
Member Author

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?)

@lonnieezell lonnieezell added the help wanted More help is needed for the proper resolution of an issue or pull request label Jul 11, 2016
@martinservices
Copy link

GraphicsMagick would be great, imagemagick is slow and for providers is the new fork way better

@AkenRoberts
Copy link

Recommend that there is a REAL solid game plan with how image manipulation is instantiated and interacted with, with emphasis on:

  • Not having the primary "library" class (in CI3 terms) be the wrapper around a single image. Instead, the primary library should be a factory that allows instantiation of individual image wrappers.
  • Whether the individual image wrappers should be mutable or immutable.

@davidgv88
Copy link
Contributor

Excuse my ignorance...

Why can not use for CI4 a third party library for example this:

http://image.intervention.io

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....???

@lonnieezell
Copy link
Member Author

@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!

@AkenRoberts
Copy link

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?

@lonnieezell
Copy link
Member Author

And playing the normal Devil's Advocate to your Devil's Advocate:

. If good user-land packages already exist, what benefits are you providing the community by creating another?

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...

@martinservices
Copy link

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.

@davidgv88 davidgv88 mentioned this issue Sep 14, 2016
@Portaflex
Copy link
Contributor

#279

@lonnieezell lonnieezell added in progress and removed help wanted More help is needed for the proper resolution of an issue or pull request labels Apr 18, 2017
@lonnieezell lonnieezell self-assigned this Apr 18, 2017
@Xirt
Copy link

Xirt commented Apr 27, 2017

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:

thumb

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
In the old CI 3.x it is only possible to save or output the result of a modification directly (via the configuration "dynamic_output"). It would be great to extend this functionality to simply return the image so another modification can be done on the image (e.g. chaining). For e.g. $image->resize()->blur();.

Gaussian blur
Furthermore, I have written a method to add a gaussian blur to an image for my CI 3.x library (using GD2). Unfortunately, I found out that a simple gaussian blur filter in PHP takes quite some resources. After searching I found an alternative methodology here which I have used to write below method. I hope similar functionality can be included in the CI 4.x library to add to its usefulness.

/**
 * 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;

}

@davidgv88
Copy link
Contributor

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');

Result:
example_resized

@lonnieezell
Copy link
Member Author

@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).

@Xirt
Copy link

Xirt commented Apr 28, 2017

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.

@lonnieezell
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature PRs for new features
Projects
None yet
Development

No branches or pull requests

7 participants