I'm trying to downsample every image that I consider large for my site on uploading, so when a user tries to upload an image I firstly check if it has an acceptable resolution, otherwise I want to drop this resolution. The code I use for this is:
if ($image->isValid()){
$imagick = new \Imagick();
$imagick->readImage($image);
$resolution = $imagick->getImageResolution();
$resolution_x = $resolution['x'];
$resolution_y = $resolution['y'];
if ($resolution_x > 30 && $resolution_y > 30){
$imagick->setImageResolution($resolution_x,$resolution_x);
$imagick->resampleImage($resolution_x/2,$resolution_x/2,\imagick::FILTER_CATROM,1);
}
$imagick->writeImage($uploadDir.$path);
}
This code was supposed to set the resolution of an image with resolution 300 dpi for example to 150dpi. Instead of this, the resolution remains 300 dpi and the image dimensions drop to the half of their previous values (e.g an image (1200x800) turns into (600x400)). Am I missing something about the functionality of Imagick::resampleImage or is there any error in my code? I've done a lot of search before post this question and tried lot of different ways to succeed my goal using Imagick but I cannot get it done!
The 'resolution' in the setImageResolution and getImageResolution functions refer to the dots per inch setting of the image, which is a hint to printers of what size to print the image i.e. how many dots per inch it should be printed at.
It does not affect the pixel dimensions of the image, and so does not have a noticeable effect on the image on a computer, which does not use the DPI setting to render an image.
You want to use either just $imagick->getImageWidth() and $imagick->getImageHeight() or $imagick->getImageGeometry() to get the pixel size of the image, and then resample it based on those pixel dimension, rather than the printer hint setting.
It sounds like the resolution values should be the same in both setImageResolution and resampleImage. Have you tried this yet?
$imagick->setImageResolution($resolution_x/2,$resolution_x/2);
Related
So here's what I'm trying to do, basically when the users upload a new image, I want to make an image half that size (keeping the proportion) and half the resolution, but save both versions. Maybe save the original as 'image-upload.jpg' and the one I modify using php save it as 'image-upload-halved.jpg'
I've messed around with wordpress filters, but can't seem to get it. Below is along the lines of what I was thinking I should do, but I really have no idea.
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
// here I was hoping I could do the image manipulation
// and also save both versions of the image
}
Any advice or links to other wordpress filters that might fit the job better would be awesome, too.
Thanks!
Take a look at the documentation for add_image_size https://developer.wordpress.org/reference/functions/add_image_size/
You should be able to add a new image size like this:
add_image_size( 'custom-size', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode
Replace "custom-size" with a name for your size and the pixel values that you want.
You can call the image in your template like this:
// Assuming your Media Library image has a post id of 42...
echo wp_get_attachment_image( 42, 'your-custom-size' );
I am using imagemagick to crop an image (using the PHP interface, but i don't think that matters too much).
I wish to crop an image, but if the crop portion goes over the image, I want it to show a background colour.
Here is the code I have so far:
$newImg = new Imagick($imgUrl);
$newImg->cropImage($cropW, $cropH, $x, $y);
$newImg->resizeImage($resizedW, $resizedH, Imagick::FILTER_CATROM, 1);
$newImg->writeImage($output_filename);
However for some reason, imagemagick refuses to show any portion of the image that is further than boundary of the image (i.e. if x and y is larger than the original image width and height, it pushes it down into view of the image).
e.g.
I want it so that if x and y is beyond the image portion, it shows a background color instead. Thanks!
UPDATE
Thanks to namelivia suggestion I decided to use the "extent" tool.However I am unable to set a background colour using this tool through PHP. For example, the following produces a larger image but with a black background, NOT purple.
$newImg = new Imagick($imgUrl);
$newImg->setImageBackgroundColor('#e7609a'); //Doesn't return an error (i.e. returns true) but also does not work!
$newImg->setImageExtent(2000, 2000);
$newImg->writeImage($output_filename);
UPDATE 2
Seems like you should use extentImage (NOT setImageExtent) if you wish to use a background color.
I think you should use the extent option first, using extent you can also pick a background color for the area "behind" then you can crop the extended image.
I am making an avatar script from scratch and am having some problems. I got transparency working, and multi-image support for heads, bodies, shirts, etc.
Anyhow, I want to be able to generate specific sizes of the avatar within the PHP script. At this time, I have the variable $baseImage, which is an image generated using the GD script below:
$baseImage = imagecreatefrompng($startAsset);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);
... combine all images into $base here
header("Content-type: image/png");
imagepng($baseImage);
The size of the image this generates is 350x550 (pixels) and I want to be able to get a smaller size.
I've done research but cannot find a working solution. What built-in PHP GD functions can resize this, retain transparency, and keep the great quality/colors?
There is no way to change the size of an image resource directly. Instead, you need to create a new image of the desired size and use imagecopyresampled to copy from the fullsize image to the resized one.
I'm searching for a function in PHP which extract the histogram from an image to an PNG file. This PNG file will be located in a different folder than the actual image and the function must handle large images (over 3 MB). I did find a function almost similar to my request but the function can not handle large images and it didn't showed the histogram nor the image as showed on their website (it showed only a blank window with a border).
I hope that you guys can help me with this.
Thanks in advance.
We've been using this one for our projects:
http://www.histogramgenerator.com/
We did not experience issues with large images. It's not free, but we definetly feel it's
worth the money we paid for. The class also offers many additional interesting features.
Regards
It is a script to draw a simple histogram like Photoshop does (only similar, because I suspect it scale both axes with a sigmoid function, or something like that).
I wrote a scale() function where you can use a last bool argument to do a linear histogram, or use a square root scale to boost low values.
<?php
//Just in case GD needs more memory
ini_set('memory_limit', '64M');
$filename='image1.png';
//Attempt to open
[$width, $height, $type]=getimagesize($filename);
if($type==IMAGETYPE_PNG){
$img=imagecreatefrompng($filename);
}
//Histogram initialization
$hist = array(
'red'=>array_fill(0,256,0),
'green'=>array_fill(0,256,0),
'blue'=>array_fill(0,256,0)
);
//Counting colors
for($x=0;$x<$width;++$x){
for($y=0;$y<$height;++$y){
$bytes=imagecolorat($img,$x,$y);
$colors=imagecolorsforindex($img,$bytes);
++$hist['red'][$colors['red']];
++$hist['green'][$colors['green']];
++$hist['blue'][$colors['blue']];
}
}
//Drawing histogram as a 256x128px image
$width=256;
$height=128;
$newimg=imagecreatetruecolor($width,$height);
//Max frequency for normalization
$maxr=max($hist['red']);
$maxg=max($hist['green']);
$maxb=max($hist['blue']);
$max=max($maxr,$maxg,$maxb);
function scale($value,$max,$height,$scale=FALSE){
$result=$value/$max; //normalization: value between 0 and 1
$result=$scale?$result**0.5:$result; //sqrt scale
$result=$height-round($result*$height); //scaling to image height
return $result;
}
$top=220; //255 seems too bright to me
for($x=0;$x<$width;++$x){
for($y=0;$y<$height;++$y){
$r=($y>scale($hist['red'][$x],$maxr,$height,TRUE))?$top:0;
$g=($y>scale($hist['green'][$x],$maxg,$height,TRUE))?$top:0;
$b=($y>scale($hist['blue'][$x],$maxb,$height,TRUE))?$top:0;
$colors=imagecolorallocate($newimg,$r,$g,$b);
imagesetpixel($newimg,$x,$y,$colors);
}
}
//Saving the histogram as you need
imagepng($newimg,'.subfolder/histogram.png');
//Use the next lines, and remove the previous one, to show the histogram image instead
//header('Content-Type: image/png');
//imagepng($newimg);
exit();
?>
Note I'm not checking if filename exist, neither if getimagesize() or imagecreatefrompng() failed.
I tested this with a 2MB (5800 x 5800) PNG Image. Basicaly the "imagecreatefrompng()" method is consuming lot of memory.
So before making the call, I increased the memory al the way up to 512M and set the execution time to 5 mins
ini_set('memory_limit', '512M');
set_time_limit(5*60);
After the Image is created, restore the memory limit
$im = ImageCreateFromPng($source_file);
ini_restore('memory_limit');
Reference: http://www.php.net/manual/en/function.imagecreatefrompng.php#73546
I have a site where users can upload images. I process these images directly and resize them into 5 additional formats using the CodeIgniter Image Manipulation class. I do this quite efficiently as follow:
I always resize from the previous format, instead of from the original
I resize using an image quality of 90% which about halves the file size of jpegs
The above way of doing things I implemented after advise I got from another question I asked. My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me.
Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. The idea being that that file too will take half the file size. I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. Here's my code, somewhat simplified:
$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);
$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();
The above code works fine for all formats except the original. I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. Next, it simply makes a copy of that file without processing it at all. I commented that piece of code to force it to resize but now still nothing happens.
Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions?
I guess you could do something like this:
$original_size = getimagesize('/path/to/original.jpg');
And then set the following options like this:
$resize_settings['width'] = $original_size[0];
$resize_settings['height'] = $original_size[1];
Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options:
Rotate the Image by 360ยบ
Watermark the Image (with a 1x1 Transparent Image)
Do It Yourself
The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:
ImageJPEG(ImageCreateFromString(file_get_contents('/path/to/original.jpg')), '/where/to/save/optimized.jpg', 90);
As you can see, it's even more simpler than using CI.
PS: The snippet above can open any type of image (GIF, PNG and JPEG) and it always saves the image as JPEG with 90% of quality, I believe this is what you're trying to archive.