Adding watermarks to images using alpha channels in wordpress - wordpress

I find the code below on php.net and it is working perfectly when i add the images mentioned in code ($stamp & $im)
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
but my question is that how to use or implement this code in Wordpress post image fetched using
wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )
also some one please explain me what is happening here. where to add this code? how to make all wordpress post images watermarked with this code.
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

You could use in below hook:
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
//your code goes here.
return $file;
}
Reference
Hook into Wordpress image upload

Related

wordpress apply_filters wp_mime_type_icon not working

I doing some quick test by replacing the standard video.png icon at /wp-admin/upload.php with the ready video snapped thumbnail.
So that in future, I can easily know the video is related to what topic by seeing the image, not the video icon....
I test it with quick code, but failed to replace video.png.
Any wordpress expert can give some clue what might go wrong.
$converted_video_attachment_id = wp_insert_attachment($converted_video_attachment, $converted_video_path, $post_id);
add_filter('wp_mime_type_icon', 'change_mime_icon', 10, 3);
$thumb = "https://nationaltoday.com/wp-content/uploads/2020/08/international-cat-day-640x514.jpg";
$mime = "image/jpeg";
apply_filters( 'wp_mime_type_icon', $thumb, $mime, $converted_video_attachment_id );
function change_mime_icon($thumb, $mime = null, $post_id = null){
return $thumb;
}
From the edit button of the video ob_mp4_cc_6331bf30bbd51, I right click get this id 554,
/wp-admin/post.php?post=554&action=edit
So I assign $converted_video_attachment_id = 554 to test.
But the video icon won't change to image display.

Modify uploads directory for gutenberg block

I actually create an extranet space on my website, with pages where you must to be logged-in to see the content.
My problem is about media uploads.
I want to make them private for a specific custom post type, and upload them in a sub directory (uploads/private).
I use the upload_dir filter (add_filter('upload_dir', 'extranet_upload_directory');) with success for thumbnail for exemple, but when I upload an image in the content with a gutenberg block, this one is upload in the initial foldier (uploads), not in uploads/private.
Does anyone have an idea where to look at ?
My code below :
add_filter('upload_dir', 'extranet_upload_directory');
function extranet_upload_directory( $param ){
$id = $_REQUEST['post_id'];
if ( ( get_post_type( $id ) == 'test-extranet' ) ) {
$param['subdir'] = '/private' . $param['subdir'];
$param['path'] = $param['basedir'] . $param['subdir'];
$param['url'] = $param['baseurl'] . $param['subdir'];
}
return $param;
}
This may be a bug... I have a plugin that offers the same functionality as yours and noticed the following:
While creating a post of that new custom post type (e.g. PATHTOsite.fr/wp-admin/post-new.php?post_type=post_type_name):
When I immediately press upload after opening an new image block the uploaded image goes to default directory, uploads.
but, if I first press 'media library' after opening an new image block and then go to the upload button there and upload the image there, the image will be uploaded in the modified uploads directory.

WordPress: Always create an image for custom image size, even if the uploaded image is smaller than the custom size

I want to generate an extra image size in WordPress. WP lets you use add_image_size like this:
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
If the image has the min. width of 2000px, the image size will be generated.
However if the image is smaller the custom size won't be generated.
Is there any option to create an image for the custom image size everytime, no matter of the uploaded image size? Or maybe its possible to additionally save the original image?
To explain why I am trying to achieve: I need a watermark on my images, but need also the original image (or a very big image) as well without the watermark.
I also tried with the "large" image, but here the same problem. By uploading an image smaller than 1024px, the image size wouldn't be created.
You can hook into wp_generate_attachment_metadata and make a copy of the uploaded image there if it wasn't already created. I've included the code to do this below.
Add this to your functions.php. Each step is commented to explain what its doing, but basically during the upload process:
we check if WP created your custom size
If it was, do nothing
Otherwise create a copy of the image called imagename-WxH.extn (e.g.if the uploaded image was cat.jpg and was 700x520px, our copy will be called cat-700x520px.jpg)
Add the copied image as your custom size original-img
Then you can use your original-img custom size in your code and always have an image.
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
// Hook into the upload process
add_filter('wp_generate_attachment_metadata','copy_original_img');
// Check if the original image was added, if not make a copy and add it as original-img
function copy_original_img($image_data) {
// if the original-img was created, we don't need to do anything
if (isset($image_data['sizes']['original-img']))
return;
// 1. make a copy of the uploaded image to use for original-img
$upload_dir = wp_upload_dir();
$uploaded_img = $image_data['file'];
$img_w = $image_data['width'];
$img_h = $image_data['height'];
// construct the filename for the copy in the format: imagename-WxH.extn
$path_parts = pathinfo($uploaded_img);
$new_img = $path_parts['filename']."-".$img_w."x".$img_h.".".$path_parts['extension'];
// make a copy of the image
$img_to_copy = $upload_dir['path'] . '/'.$uploaded_img;
$new_img_path = $upload_dir['path'] . '/'.$new_img;
$success = copy($img_to_copy,$new_img_path);
// 2. If the image was copied successfully, add it into the image_data to be returned:
if ($success){
$image_data['sizes']['original-img']['file'] = $new_img;
$image_data['sizes']['original-img']['width'] = $img_w ; // same as uploaded width
$image_data['sizes']['original-img']['height'] = $img_h; // same as uploaded height
$image_data['sizes']['original-img']['mime-type'] = mime_content_type($new_img_path);
}
return $image_data;
}

Set default image for woocommerce products

In my project I have many products whose image is not available till now. So I want to show custom image for those products who don't have image.
Is there a way to set default image for products (in case if product don't have image).
I solved it by adding following code in functions.php file
add_action( 'init', 'custom_fix_thumbnail' );
function custom_fix_thumbnail() {
add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/2012/07/thumb1.jpg';
return $src;
}
}
NOTE : Do not type complete url of image. Instead of complete url, use only short url as shown in code.
Original Post
TLDR;
You need to upload an image via default Wordpress Media uploader, then copy its URL (or just ID) and paste it to "WooCommerce->Settings->Products->Placeholder image"
Found an answer here: https://www.iqcomputing.com/support/articles/changing-the-woocommerce-default-image/

Wordpress , display resized image in theme with PHP

I'm coding a shortcode that accept one media ID as an attribute. This shortcode should then display the image at an existing size registered with add_image_size. Problem : my image is not displayed at the correct size.
Explanations :
My media image has been uploaded in the WP library. The original file size is huge (1227x924). The media has the ID 294.
I registered an image size in functions.php :
function my_after_setup_theme(){
add_image_size('my-image-size', 210, 136, true);
}
add_action('after_setup_theme', 'my_after_setup_theme');
I insert my shortcode in one of my pages : [my_shortcode imageid="294"]
My shortcode code is :
function my_shortcode_func($atts)
{
$a = shortcode_atts(array(
'imageid' => 0,
), $atts);
if ($a['imageid'] == 0) {
// default placeholder image
$img = 'img src="http://placehold.it/210x136"/>';
} else {
// get resized (NOT WORKING !)
$img = wp_get_attachment_image($a['imageid'], 'my-image-size');
}
return $img;
}
add_shortcode('my_shortcode', 'my_shortcode_func');
I expect the original image to be resized at the correct size (ie : 210x136) in a new thumbnail file. Instead, the $img displays the original image (1227x924) , displayed at an intermediate size via the width and height attributes of the HTML <img> tag :
What am I doing wrong ?
thanks for help.
Did you register the thumbnail size after uploading the media? If so, you will need to use a plugin to regenerate the thumbnails.
Additionally, I don't believe you need to wrap thumbnail size declarations as you have done, I have the following;
if (function_exists('add_theme_support'))
{
add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit.
add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need
}
So it would be worth checking if your thumbnails are actually be generated to start with, as I have never done it the way you have defined in your question.

Resources