What to do: add a new image size and grayscale this specific size.
What I've done so far:
I've been searching around and found this helpfull URLs:
wpbeginner, Ryan Berry. The second solution creates a grayscale image for each size on every new image and is nearly what I'm searching for.
The Problem: I know how to add a new image size and the above functions creating grayscale images. What I don't know is how to add this filter just to a specific size.
Using the code from wpbeginner, it seems it only grayscales image which is specified as themename-bw-image, as seen from here (line 04):
$file = trailingslashit($file['path']).$meta['sizes']['themename-bw-image']['file'];
So I suppose if you don't want the image to get grayscaled, you could add a new image size:
add_action('after_setup_theme','themename_bw_size');
function themename_bw_size() {
add_image_size('themename-bw-image', 100, 100, true);
add_image_size('other-nongrayscaled-image', 150, 150, true);
// etc...
}
Related
I am trying to resize a pdf (which i converted from image), and I am trying to resize (increase) the image in its ratio.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC, 1, true);
// and this:
// $imagick->adaptiveResizeImage(595,842, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
But the image is getting blurry. However, it's not a bad quality image and the image can go that size without getting disturbed. (For example, if I let Twilio to do it (via fax api), the same image can get to that scale).
I have also tried with putting blur parameter of resizeImage between '1' and '0.1'
Original pdf (you can also see it here, if you want to try):
My resized pdf (with blur 1):
My "adaptive resized" pdf:
You are starting with a very small pdf if rasterized and enlarging. So that would cause blurring. But if you increase the input density, it works fine for me in ImageMagick as
convert -density 600 input.pdf -resize 595x842 result.png
I do not know Imagick well, but try the following. Reduce the blur value in resizeImage as desired to make it sharper.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->Imagick::setImageResolution( 600, 600 );
$imagick->resizeImage(595,842,\Imagick::FILTER_CATROM, 1, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
We are processing nearly 20M existing images (not files), all of which have been converted to the raw PNG data. Each image is always 640x480, and we want them all to be saved as square 640x640 files, with the original image on the top of the larger canvas, leaving the additional lower 160px for descriptive text to be added.
Our PNG data being retrieved is like this:
data:image/png;base64,iVBORw0 ... kJggg==
I've seen lots of posts and examples showing work with resizing, transparencies, colors, etc., but cannot determine where to start creating a new empty 640x640 "canvas" with existing PNG data.
If this is a duplicate question, I'd be happy to delete it if I can get some direction on how to begin. Without knowing the basics to get started, googling things like "image create png canvas" hasn't been helpful. Clearly, image processing is new to me, so apologize if I'm not being clear enough. Thanks in advance.
This should work for you:
$imgData = 'data:image/png;base64,iVBORw0 ... kJggg==';
$img = imagecreatefromstring(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imgData)));
$targetImage = imagecreatetruecolor(640, 640);
imagecopyresampled($targetImage, $img, 0, 0, 0, 0, 640, 480, 640, 480);
imagepng($targetImage, $yourPath);
Of course you might want to add some checks, background color etc. but I think you will be ready to start with this.
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 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 want images to resize after upload in 4 different formats. If i resize it to best fit(i.e aspect ratio) some images come too small if height or width is too large than the other and if i resize it to fixed size then images get skewed. So what is the best way to resize a image. I am currently doing this using via imagemagik thumbnailImage() but i think it's a general problem. What are sites like google or facebook doing. what is the best thing to do in that case
You can use resize functionality for resize image in different size during upload image.
For example:
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['uploaded_image']['tmp_name']);
$image->resizeToWidth(300);
$image->resizeToHeight(200);
$image->save('resizeImage.jpg'
Similarly, you can save image in different size.
For more in detail you can find here:
http://sanjeevkumarjha.com.np/how-to-resize-and-crop-image/
You can also use ImageWorkshop: http://phpimageworkshop.com/doc/17/resizing.html
$layer = new ImageWorkshop(array("fileObject" => $_FILES["uploadedImage"]));
$layer->resizeInPixel(200, 150, true); // Conserve proportion !
$layer->save(__DIR__."/web/uploads/2012", "thumb.png", true, null, 95);
You will have a resized picture of 200px/150px with conserved proportion !