Service to resize uploaded images to a Wordpress site

Asked

Viewed 621 times

2

I have a Wordpress site which has the biggest headache to resize images, and I think a PHP script that resizes images is not the solution for me.

Every post involves about 3 images, it is very tiring to create multiple resizes; there is some website on the web that allows me to upload the photos and choose the width size, e.g.: width: 250px?

When I send an image to highlight it gets the original resolution, if it is a 4.000px x x 2.000px image it will get very heavy. I don’t need an image of this size, what I want is just to reduce the resolution of the image (pixels), thus making the images light.

Or there is some Wordpress plugin that helps me in something?

  • 3

    As much as you said a PHP script is not a solution, I was curious to know why. In your case, if the application is running on a Linux server, you could easily solve it with Imagemagick, see: http://www.imagemagick.org/script/mogrify.php. You can even resize images in batch instead of one by one.

  • Is that I need to have the file (.png/.jpg/.gif), to upload, and not a php script, what I need is the image (file) resized.

  • Excuse me, but I still don’t understand your problem. Is the site yours or are you a user of this site? I don’t think the links below will help you, because you can only resize one image at a time. For three images, three distinct uses will be required in both options listed in the answer below.

  • 1

    The @Cantoni remark is the definition of XY problem...

  • 1

    I use the Wordpress. I am the owner of the site, but when I send an image to highlight it is with the original resolution, if it is an image 4.000px x x 2.000px it will be very heavy, it is not necessary an image of this size, what I want is just reduce the resolution of the image (pixels), thus making the images light. Or you know some wordpress plugin that helps me in something?

  • 1

    @Alexandrelopes, according to your comment above, I created an answer, because I found it more appropriate for the context of your question.

  • I recommend that @Alexandrelopes also read about the XY problem, since it has been common this kind of question on Sopt.

Show 2 more comments

3 answers

4

According to the comments described in the question, I understood the problem and I believe that an interesting solution is to use Imagemagick, because of that I created this answer.

There is Imagemagick port for Windows and can be downloaded here:

Imagemagick for Windows

Some of the advantages of Imagemagick over any web service are:

  1. Runs locally. There is no risk of the images in question stopping in search indexers. Remember that these web services receive your image, do the processing (probably with Imagemagick or similar) and return a processed image. There is no guarantee that the images stored there can not one day stop in search indexers like Google, etc. This same principle applies to the famous PDF to DOC converters, etc.; Therefore, if the file to be converted is confidential, never use any web service.

  2. Local processing tends to be faster, since these services, because they are free, usually do not run on powerful servers;

  3. You can process in a batch of images (for example, a folder with multiple Jpgs). Very useful when you want to send your images to Picasa, for example. You reduce it to 1600x1200, which makes uploading faster;

The program should be used per command line, see some examples below:

mogrify -resize 1600x1200 *.jpg

The above example resizes all files in the current folder to 1600x1200 resolution. Note that files after processed will have the same name as the original (files will be lost). Therefore, ensure that the command will not run in the original files.

mogrify -path imgs/ -resize 800x600 *.jpg

The above command will reduce the files in the current folder to 800x600 resolution, however the processed files will be saved in the imgs folder (which, in the above case, must be inside the current folder). Thus, there is no risk of the original files being modified.

There are several other options on the command line and this is one of the great advantages of Imagemagick.

Some examples of using Imagemagick More Examples

  • Cantoni Could you take a look at this other question? http://answall.com/questions/20662/comorinstalar-imagemagick-no-windows-7-32-bits Thank you!

  • @Alexandrelopes, did you have any trouble using Imagemagick?

  • No! Round!! Perfect.

3

1

A plugin is pretty easy:

<?php
/**
 * Plugin Name: (SOPT) Substituir Imagens Originais
 * Description: Substitui imagens originais (full size) pelo tamanho "grande" na hora do upload. 
   Ajuda a economizar espaço em disco, já que uma foto de 3240x4320 (5MB) pode se converter
   em uma de 768x1024 (108KB - exemplo depende do ajuste em /wp-admin/options-media.php e da qualidade jpeg). 
 * Author: brasofilo
 * Plugin URI: https://wordpress.stackexchange.com/a/54059/12615
 */

add_filter( 'wp_generate_attachment_metadata','replace_uploaded_image_wpse_48882' );
add_filter( 'jpeg_quality', 'qualidade_jpeg_sopt_19538' );

/**
 * Replace original by largest size on upload
 * 
 * @author brasofilo
 * @wp_hook wp_generate_attachment_metadata
 * 
 * @param object Image information
 * @return object
 */
function replace_uploaded_image_wpse_48882( $image_data ) 
{
    // if there is no large image : return
    if ( !isset( $image_data['sizes']['large'] ) ) 
        return $image_data;

    // paths to the uploaded image and the large image
    $upload_dir              = wp_upload_dir();
    $uploaded_image_location = $upload_dir['basedir'] . '/' . $image_data['file'];
    $large_image_location    = $upload_dir['path'] . '/' . $image_data['sizes']['large']['file'];

    // delete the uploaded image
    unlink( $uploaded_image_location );

    // rename the large image
    rename( $large_image_location, $uploaded_image_location );

    // update image metadata and return them
    $image_data['width']  = $image_data['sizes']['large']['width'];
    $image_data['height'] = $image_data['sizes']['large']['height'];
    unset($image_data['sizes']['large']);

    return $image_data;
}

/**
 * Qualidade: de 0 a 100
 */
function qualidade_jpeg_sopt_19538() 
{
    return 75;
}

It is also possible to set the quality of JPEG so advanced and use a custom size:

add_image_size( 'new-large', 1600, 1200 ); /* definido em functions.php ou num plugin */
  • exchanging all occurrences of $image_data['sizes']['large'] for $image_data['sizes']['new-large'].
  • Hello @brasofilo, thank you so much for the contribution, however, as I mentioned above, I need the resized image to upload, so this is not the solution. More surely your great response will help several other people. At the moment I’m still using the program Imageresizer. But brasofilo, you could better detail the plugin? How it works, etc... It seems to be excellent! : D

  • 1

    Of course, because doing it locally is the solution. Upload to resize, download and then upload back to the site doesn’t seem cool... Do you think the plugin description within the insufficient code? Maybe it is not obvious first and best put in the reply text?

  • Yes, bras! D :§

Browser other questions tagged

You are not signed in. Login or sign up in order to post.