Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Kupası
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPress’te Görüntüler Nasıl Gri Tonlanır

WordPress’te resimleri yüklediğinizde otomatik olarak gri tonlama yapmanın bir yolu olup olmadığını merak ediyor musunuz?

Resimlerinizi gri tonlamaya dönüştürmek için genellikle bir fotoğraf düzenleme aracına ihtiyacınız vardır. Ancak, web sitenize yüklemeden önce her bir görseli tek tek düzenlemeniz gerekeceğinden bu zaman alıcı olabilir.

Bu makalede, WordPress’te görselleri sitenize yüklerken nasıl gri tonlama yapacağınızı göstereceğiz.

Greyscale Images in WordPress

WordPress’te Gri Tonlamalı Görselleri Ne Zaman Kullanmalısınız?

Gri tonlamalı görüntüler yalnızca resimdeki ışık miktarı hakkında bilgi içerir. Görüntü renkleri, siyah ve beyaz arasında değişen farklı gri tonlarını gösterir.

Belirli durumlarda, gri tonlamalı görseller kullanmak WordPress web siteniz için faydalı olabilir. Örneğin, görselde gösterilen nesnelerin okunabilirliğini artırmak için kullanabilirsiniz.

Öte yandan, gri tonlamalı görüntüler küçük boyutları nedeniyle görüntü işleme için yaygın olarak kullanılır. Geliştiricilerin karmaşık işlemleri daha kısa sürede çalıştırmasına olanak tanır.

Bununla birlikte, resimlerinizi yüklediğinizde WordPress’te nasıl gri tonlamaya dönüştürebileceğinizi görelim.

WordPress’te Yüklenen Görüntüleri Grileştirme

WordPress’e resim eklemek söz konusu olduğunda, yüklemeden önce Photoshop gibi bir fotoğraf düzenleme yazılımı kullanarak bunları düzenlemeniz ve renkli resimleri gri tonlamaya dönüştürmeniz gerekir.

Yüklemeniz gereken yüzlerce veya binlerce resim varsa, her bir resmi manuel olarak düzenlemek çok zaman alabilir.

Ancak, yükleme sırasında bunları otomatik olarak gri tonlamalı görüntülere dönüştürebilirsiniz. Başlamak için tek yapmanız gereken aşağıdaki kodu temanızın functions.php dosyasına eklemektir:

add_filter('wp_generate_attachment_metadata','rb_bw_filter');
   
function rb_bw_filter($meta) {
   
    $path = wp_upload_dir(); // get upload directory
    $file = $path['basedir'].'/'.$meta['file']; // Get full size image
   
    $files[] = $file; // Set up an array of image size urls
   
    foreach ($meta['sizes'] as $size) {
        $files[] = $path['path'].'/'.$size['file'];
    }
   
    foreach ($files as $file) { // iterate through each image size
   
        // Convert image to grayscale credit to http://ottopress.com/2011/customizing-wordpress-images/
   
        list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
        $image = wp_load_image($file);
        imagefilter($image, IMG_FILTER_GRAYSCALE);
        switch ($orig_type) {
            case IMAGETYPE_GIF:
                imagegif( $image, $file );
                break;
            case IMAGETYPE_PNG:
                imagepng( $image, $file );
                break;
            case IMAGETYPE_JPEG:
                imagejpeg( $image, $file );
                break;
        }
    }
    return $meta;
}

Tema dosyalarına kod eklemenin basit bir yolu WordPress için WPCode eklentisini kullanmaktır.

Temanızın function.php dosyasını manuel olarak düzenlemenize gerek kalmadan kod parçacıklarını çalıştırmanıza yardımcı olan en iyi kod parçacıkları eklentisidir. Bu şekilde, sitenizi bozma konusunda endişelenmenize gerek kalmaz.

WPCode

Öncelikle, ücretsiz WPCode eklentisini indirip sitenize yüklemeniz gerekir. Yardıma ihtiyacınız varsa, lütfen bir WordPress eklentisinin nasıl kurulacağına ilişkin kılavuzumuzu izleyin.

Not: WPCode’un ücretsiz sürümü WordPress’e kolayca özel kod eklemek için ihtiyacınız olan her şeyi sunar. Özel bulut snippet kütüphanesi, zamanlanmış snippet’ler, dönüşüm pikselleri ve daha fazlası gibi daha gelişmiş özellikler için WPCode Pro‘ya yükseltebilirsiniz.

Etkinleştirmenin ardından WordPress panonuzdan Code Snippets ” + Add New seçeneğine gidebilirsiniz.

Ardından, ‘Özel Kodunuzu Ekleyin (Yeni Snippet)’ seçeneğine gidin ve ‘Snippet kullan’ düğmesine tıklayın.

Add a new custom code snippet in WPCode

Ardından, devam edin ve snippet’iniz için bir ad girin ve yukarıdaki kodu ‘Kod Önizleme’ alanına yapıştırın.

Ayrıca sağdaki açılır listeden kod türü olarak ‘PHP Snippet’ seçmeniz gerekir.

Add code snippet for grayscale images

Kodu girdikten sonra, ‘Ekleme’ bölümüne ilerleyebilirsiniz.

Burada, ‘Otomatik Ekle’ seçeneğini seçili bırakabilirsiniz. Bu, kodu sizin için otomatik olarak ekleyecek ve yürütecektir.

Insertion methods for snippets in WPCode

Son olarak, ekranın üst kısmına geri dönün ve anahtarı ‘Etkin Değil’den ‘Etkin’e getirin ve ‘Snippet’i Kaydet’ düğmesine tıklayın.

Save and activate your custom code snippet

Ardından, yeni bir sayfa düzenleyerek veya ekleyerek kodu test edebilirsiniz. WordPress editöründeyken, ‘+’ düğmesine tıklayın ve bir Resim bloğu ekleyin.

Artık WordPress blogunuza herhangi bir görüntü yükleyebilirsiniz ve otomatik olarak gri tonlamalı bir görüntüye dönüşecektir.

Convert images to grayscale on upload

Umarız bu makale WordPress’te görselleri nasıl gri tonlayacağınızı öğrenmenize yardımcı olmuştur. Ayrıca en iyi blog platformunun nasıl seç ileceğine dair rehberimize ve en iyi web tasarım yazılımı uzman seçimlerimize de göz atabilirsiniz.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Açıklama: İçeriğimiz okuyucu desteklidir. Bu, bazı bağlantılarımıza tıklarsanız komisyon kazanabileceğimiz anlamına gelir. WPBeginner'ın nasıl finanse edildiğini, neden önemli olduğunu ve nasıl destek olabileceğinizi görün. İşte editoryal sürecimiz.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Ultimate WordPress Araç Kiti

Araç setimize ÜCRETSİZ erişim sağlayın - her profesyonelin sahip olması gereken WordPress ile ilgili ürün ve kaynaklardan oluşan bir koleksiyon!

Reader Interactions

18 yorumBir Cevap Bırakın

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Tomas Kapler

    Great tip, i would just like to mention, that for many usages it might be better to simply do this via CSS filter, e.g.
    img.bw {
    filter: grayscale(1);
    }
    you can even e.g. show b/w by default and color on hover, or you can do animation from greyscale to full color and back, e.g.
    img.bw {
    filter: grayscale(0);
    }

    img.bw.grey {
    filter: grayscale(1);
    transition-property: filter;
    transition-duration: 1s;
    }

    i also do not think, that wp_generate_attachment_metadata filter is the proper one, that should be used because of its calling in meta creation not only image creation and making second image manipulation, but the proper solution would be much longer, so i understand why it is done this way

  3. rok

    It works nice but when uploading, WP 4.3 throws error in media library.
    i think have applied all fixes written in comments, but still error.

    my code looks like this:

    add_filter(‘wp_generate_attachment_metadata’,’themename_bw_filter’);
    function themename_bw_filter($meta) {
    $time = substr( $meta[‘file’], 0, 7); // <- get the correct time of the upload
    $file = wp_upload_dir( $time ); // <- locates the correct upload directory
    $file = trailingslashit($file[‘path’]).$meta['sizes']['slide-pic']['file'];
    if( $file ) {
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
    }
    $image = wp_load_image($file);
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    switch ($orig_type) {
    case IMAGETYPE_GIF:
    imagegif( $image, $file );
    break;
    case IMAGETYPE_PNG:
    imagepng( $image, $file );
    break;
    case IMAGETYPE_JPEG:
    imagejpeg( $image, $file );
    break;
    }
    return $meta;
    }

  4. Kim

    Will this change all pictures in word press to grayscale or only newly updates ones?

  5. Tomaž Zaman

    I know I’m a bit late to the discussion, but I just had the same problem with an error (that others report):

    imagefilter() expects parameter 1 to be resource, string given

    This happens when you try to upload image through media modal while editing a post, older than the current month which apparently confuses WordPress which directory the original image is in and which directory it should save the grayscaled image in.

    This is the solution:


    <?php
    add_filter('wp_generate_attachment_metadata','themename_bw_filter');
    function themename_bw_filter($meta) {
    $time = substr( $meta['file'], 0, 7); // <- get the correct time of the upload
    $file = wp_upload_dir( $time ); // <- locates the correct upload directory
    $file = trailingslashit($file['path']).$meta['sizes']['themename-bw-image']['file'];
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
    $image = wp_load_image($file);
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    switch ($orig_type) {
    case IMAGETYPE_GIF:
    imagegif( $image, $file );
    break;
    case IMAGETYPE_PNG:
    imagepng( $image, $file );
    break;
    case IMAGETYPE_JPEG:
    imagejpeg( $image, $file );
    break;
    }
    return $meta;
    }

  6. endul

    how to change, result filter image to specific folder/directory

    • mklemme

      @geertvdheide

      If you wanted to add multiple size support, try what I use:

      if ( function_exists( ‘add_theme_support’ ) ){

      add_theme_support( ‘post-thumbnails’ );

      set_post_thumbnail_size( 50, 50, true );

      add_image_size( ‘medium-thumb’, 660, ”, true );

      add_image_size( ‘large-thumb’, 960, ”, true );

      add_image_size( ‘small-thumb’, 100, ”, true );

      }

      (The height is not defined, only the width.)

      you would have to add the different names to make the function apply to them all:

      [‘medium-thumb’ , ‘large-thumb’ , ‘small-thumb’] in the filter code.

      Calling the thumbnail in your theme is the same listed in the article:

    • Mike Logan

      Excellent idea, almost 10 years later this is still handy.

  7. geertvdheide

    Thanks for sharing this code! Except I’m running into a weird issue when trying to implement it. It’s related to the uploaded image’s size (pixel size, meaning its dimensions). Copied the code literally to my theme’s functions.php, and it works a charm with images larger than the size specified in the add_image_size call. But when using an image smaller than or equal to the size specified, the uploader in WordPress gives me errors and doesn’t process the image size (either from the media section of the admin environment, or from a specific post or page). The error:

    Warning: imagefilter() expects parameter 1 to be resource, string given.

    Ssome other stuff in the error as well, but this seems to be the main cause. The image data given to the imagefilter function must not be valid or doesn’t exist?

    Any idea what’s causing this? The only real difference between my situation and a clean install is that I’ve also added a few other add_image_size calls for other purposes in my site. I’m also adding the same size twice (one black/white, one regular), but that doesn’t seem to be a problem with the larger images.

    • Ed Nailor

      @GEERTVDHEIDE

      and for others that need this:

      When the script is converting the image to greyscale and the file uploaded does not fit the file size, this causes the script to break. To prevent this, simply add a quick if() conditional to make sure you have the $file.

      $file = trailingslashit($file[‘path’]).$meta[‘sizes’][‘themename-bw-image’][‘file’];
      if( $file ) {
      list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
      ——– remainder of code until ——–
      return $meta;
      }

      This will check to make sure the file size you have requested exists before it tries to convert it.

      Hope that helps!

  8. frankiegershwin

    @Otto42 Thanks for offering. I will.

  9. frankiegershwin

    @Otto42 thank you! I had a bit of a hard time, actually and had to undoe it :( will pick it up tomorrow. It’s a good way to mix it up onsite

  10. Otto42

    @frankiegershwin Lemme know if you need help. I added a way to do sepia tones too in my comments on the original post: http://t.co/avDNNEX

  11. rodhk

    @rodriguezhernan vaya idiotez de truco…. madremua tocar el codigo para eso

  12. wpbeginner

    @Otto Thanks for the comment Otto. Updated the post as well :)

  13. Otto

    Note that people should change “themename” to be the name of their theme instead.

Bir Cevap Bırakın

Yorum bırakmayı seçtiğiniz için teşekkür ederiz. Lütfen tüm yorumların yorum poli̇ti̇kasi uyarınca denetlendiğini ve e-posta adresinizin yayımlanmayacağını unutmayın. Ad alanında anahtar kelime KULLANMAYIN. Kişisel ve anlamlı bir sohbet edelim.