Coder Social home page Coder Social logo

Image Class about codeigniter4 HOT 15 CLOSED

codeigniter4 avatar codeigniter4 commented on July 28, 2024 2
Image Class

from codeigniter4.

Comments (15)

AkenRoberts avatar AkenRoberts commented on July 28, 2024 2

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?

from codeigniter4.

CodeBrauer avatar CodeBrauer commented on July 28, 2024 1

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.

from codeigniter4.

lonnieezell avatar lonnieezell commented on July 28, 2024

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

from codeigniter4.

martinservices avatar martinservices commented on July 28, 2024

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

from codeigniter4.

AkenRoberts avatar AkenRoberts commented on July 28, 2024

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.

from codeigniter4.

davidgv88 avatar davidgv88 commented on July 28, 2024

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

from codeigniter4.

lonnieezell avatar lonnieezell commented on July 28, 2024

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

from codeigniter4.

lonnieezell avatar lonnieezell commented on July 28, 2024

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

from codeigniter4.

martinservices avatar martinservices commented on July 28, 2024

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.

from codeigniter4.

Portaflex avatar Portaflex commented on July 28, 2024

#279

from codeigniter4.

Xirt avatar Xirt commented on July 28, 2024

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;

}

from codeigniter4.

davidgv88 avatar davidgv88 commented on July 28, 2024

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

from codeigniter4.

lonnieezell avatar lonnieezell commented on July 28, 2024

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

from codeigniter4.

Xirt avatar Xirt commented on July 28, 2024

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.

from codeigniter4.

lonnieezell avatar lonnieezell commented on July 28, 2024

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

from codeigniter4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.