0ورڈپریس علامتورڈپریس کے ذریعہ خودکار طور پر اییف تصاویر تیار کریں اور پیش کریں

I have spent quite a lot of time optim­ising the per­form­ance of this site, and as part of ongo­ing work I mon­it­or new tech­no­lo­gies that could help improve the speed for vis­it­ors. I’ve been track­ing the adop­tion of next-gen­er­a­tion image formats for a while and with sup­port by web browsers now fairly wide­spread it was time to fig­ure out how to make use of these new formats in wordpress.

There are mul­tiple steps required which I have broken down into head­ings below

nginx میں مدد کو فعال کریں

The first step is to enable your web serv­er (میرے معاملے میں) to recog­nise the mime types of the new formats. ایسا کرنے کے ل you آپ کو mime.types میں ترمیم کرنے کی ضرورت ہے جو /etc/nginx/mime.tyype پر ملنے کا امکان ہے۔. I added the fol­low­ing section

/ مشمولات قواعد کی خلاف ورزی کرتے ہیں;
تصویر / heif heif;
امیج / ہییک تسلسل ہیکس;
شبیہہ / heif- ترتیب heifs;
تصویر / avif avif;
تصویری / avif-sequence جائزہ;
تصویر / jxl jxl;

جہاں فائلیں دستیاب ہوں وہ خود بخود پیش کریں

The next step is to tell nginx to serve files auto­mat­ic­ally whenev­er they exist (and to fall back to older formats where a new format file does­n’t exist)
To do this first edit the main nginx con­fig file (usu­ally /etc/nginx/nginx.conf) and add the fol­low­ing sec­tion inside the http{} sec­tion of the config

نقشہ $ http_accept $ img_ext
{
   ~ شبیہ / jxl '.jxl';
   ~ شبیہ / avif '.avif';
   ~ تصویری / ویبپ '.webp';
   پہلے سے طے شدہ '';
}

Note that I am only try­ing to serve jxl (JPEG-ایکس ایل) یا avif فائلیں, آپ مزید اختیارات شامل کرسکتے ہیں (in order of pref­er­ence!) اگر آپ چاہیں.
اگلے, you need to add the fol­low­ing under the serv­er{} sec­tion of the nginx con­fig (which may be in the main con­fig file or may be in a sep­ar­ate con­fig file depend­ing how you’ve set up your nginx con­fig structure)

مقام ~ * ^. + .(PNG|JPG|jpeg)$
{
   add_ ہیڈر مختلف قبول کریں;
   try_files $ uri $ img_ext $ uri = 404;
}

Now nginx will look for image.jpg.jxl and then image.jpg.avif and then image.jpg.webp and finally image.jpg when it is asked for image.jpg by any browsers that sup­port the new­er formats. اگلا ہمیں ورڈپریس میں ان کو فعال کرنے کی ضرورت ہے

ورڈپریس میں اگلے جن شکلوں کو چالو کریں

Add the fol­low­ing code to your functions.php (چائلڈ تھیم میں مثالی طور پر یہ کریں تاکہ جب آپ اپنے تھیم کو اپ ڈیٹ کریں تو آپ کی تبدیلیاں زیادہ تحریر میں نہ آئیں)

/***************************************************\
* فعال کریں SVG سپورٹ اور دیگر جدید تصویری شکلیں *
\***************************************************/
فنکشن سی سی_مائیم_ٹائپس( $mimes )
{
   $mimes['ایس جی جی'] = 'امیج / ایس جی جی + ایکس ایم ایل';
   $mimes['ویبپ'] = 'امیج / ویبپ';
   $mimes["سرقہ"] = "امیج / غیر ہنر ';
   $mimes['heif'] = 'امیج / ہیف';
   $mimes['ہیکس'] = "اصول / ترتیب کی شبیہہ / خلاف ورزی ';
   $mimes['heifs'] = 'شبیہہ / ہیف تسلسل';
   $mimes['avif'] = 'شبیہہ / avif';
   $mimes['نوٹس'] = 'شبیہہ / avif-sequence';
   $mimes['jxl'] = 'تصویر / jxl';
   واپس $ mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

Note that I also enabled sup­port for SVG ایک ہی وقت میں تصاویر

Now you can actu­ally upload next gen format images and use them dir­ectly in word­press if you want to, but I don’t recom­mend this as many older browsers don’t sup­port them yet — and we’ve already set up nginx to serve them intel­li­gently so we should make use of that. What we want to do is to auto­mat­ic­ally gen­er­ate the new formats when we upload images (there are plu­gins that do this for webp already, but noth­ing that does it for jxl or avif yet).

libheif انسٹال کریں

This step does require access to the com­mand line of your web host, which is straight-for­ward if you run a VPS, but may not be so simple if you’re on shared host­ing in which case you might need to ask your host for support
Run the fol­low­ing bash com­mands (these are selec­ted for کانٹاوس 8, oth­er dis­tri­bu­tions may be a little different)

dnf -y install --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-8.noarch.rpm
dnf -y install libheif

ورڈپریس فنکشنس میں اپنی مرضی کے مطابق فنکشن شامل کریں (اور ان کے تمبنےل) avif کی شکل میں

As with the earli­er functions.php step I recom­mend adding this in a child theme

/*****************************\
* png اور jpg کو avif میں تبدیل کریں *
\*****************************/
add_filter( 'wp_generate_attachment_metadata', 'jps_compress_img', 10, 2 );
تقریب jps_compress_img( $میٹا ڈیٹا, $منسلک_اید )
{
   // get filepath from id
   $filepath= get_attached_file($منسلک_اید);

   // is the file a png or jpeg
   if(pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="JPG" || pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="jpeg" || pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="PNG")
   {
      // اگر ایسا ہے, run compression of the main file to avif
      shell_exec("heif-enc $ filepath -o $ filepath.avif -q 50 -ایک");
      $حصوں = preg_split('~ /(?=[^ /]*$)', $فائل پاتھ);

      // Then run compression of the thumbnails to avif
      $thumbpaths = $metadata['سائز'];
      ہر ایک کے لئے ($thumbpaths as $key => $انگوٹھے)
      {
          $thumbpath = $ انگوٹھا['فائل'];
          $thumbfullpath = $ حصے[0] . "/" . $انگوٹھے;
          شیل_کسیک("heif-enc $ thumbfullpath -o $ thumbfullpath.avif -q 50 -ایک");
      }
   }
   // give back what we got
   return $metadata;
}

//dont forget to delete the avifs if the attachments are deleted
function jps_delete_avif($منسلک_اید)
{
   $all_images = get_interedia_image_s سائز($منسلک_اید);
   ہر ایک کے لئے($all_images بطور $ Every_img)
   {
      $ہر ایک_مگ_ڈیٹ = ڈبلیو پی پی_جیٹ_ٹیچمنٹ_ییمج_ ایس سی آر($Att_id,$ہر_میگ);
      $Every_img_path = ABSPATH.'wp-content'.substr($ہر_میگ_ڈیٹ[0],strpos($ہر_میگ_ڈیٹ[0],"/اپ لوڈز /")).'.avif';
      شیل_کسیک("rm -f $ ہر_میگ_پاتھ");
   }
}
ADD_ACTION( 'ڈیلیٹ_ٹچمنٹ', 'jps_delete_avif' );

At present that is it — all images you upload will be con­ver­ted to avif cop­ies (with the ori­gin­als retained). You can regen­er­ate all of your images to cre­ate the avif files by using a plu­gin. Note — I haven’t yet set up JPEG-XL com­pres­sion as sup­port for it isn’t avail­able in main­stream browsers yet (although it is in prerelease browsers so it is com­ing very soon).

Whilst I’m cov­er­ing next gen formats and optim­isa­tion though I have one final tip — To com­press SVG gzip کے ساتھ تصاویر (or even bet­ter zop­fli and brotli). To do that requires anoth­er cus­tom function…

اضافی انعام: SVG دباؤ

/*************************************************\
* دباؤ SVG brotli اور zopfli کے ساتھ مزید تصاویر *
\*************************************************/
add_filter( 'wp_generate_attachment_metadata', 'jps_compress_vectors', 10, 2 );
تقریب jps_compress_vectors( $میٹا ڈیٹا, $منسلک_اید )
{
    // get filepath from id
    $filepath= get_attached_file($منسلک_اید);

    // is the file an svg
    if(pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="SVG")
    {
       // اگر ایسا ہے, run compression of it using zopfli and brotli
       shell_exec("zopfli --gzip $ filepath");
       شیل_کسیک("brotli --best --output = $ filepath.br $ filepath");
    }

    // give back what we got
    return $metadata;
}

نوٹ کہ آپ کو اپنے نینگس تشکیل میں gzip_static اور brotli_static فعال کرنے کی ضرورت ہوگی.

ایک نئے ورژن کیلئے خود مرتب کریں

I found that on Cen­tos the latest ver­sion of heif-enc is 1.7 which has quite a few bugs when cre­at­ing avifs. So I opted to com­pile my own new­er (1.12) اس کی بجائے اس کی تعمیر اور استعمال کریں. Doing so was a little com­plic­ated as it required the aom codec as a shared lib­rary. To do so run the fol­low­ing bash com­mands. Make sure to run them as a nor­mal user, جڑ صارف کے طور پر نہیں. Also note some of these com­mands may not be strictly needed, it took me a while to get it work­ing and I just made a note of what worked — I am by no means a linux expert!

dnf install x265 x265-devel svt-av1
export CXXFLAGS="$CXXFLAGS -FPIC"
cd ~

git clone https://aomedia.googlesource.com/aom
mkdir -p aom_build
cd aom_build
cmake ~/aom -DBUILD_SHARED_LIBS=true
make
sudo make install
cp ./libaom.so.3 /usr/bin/local/libaom.so.3

cd ~
export PKG_CONFIG_PATH=~/aom_build/
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/aom_build/
export LD_LIBRARY_PATH

git clone --recurse-submodules --recursive https://github.com/strukturag/libheif.git
cd libheif
./autogen.sh
./configure
make
sudo make install

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/bin/
export LD_LIBRARY_PATH

ایک بار جب آپ یہ کرلیتے ہیں, make sure it works by run­ning php -a and run­ning the fol­low­ing command

شیل_کسیک("/usr / مقامی / بن / heif-enc -A");

You should get a full out­put, صرف ایک نہیں 1 لائن میں خرابی. Assum­ing that works ok you can modi­fy your functions.php so that each of the shell-exec ref­er­ences points to /us­r/­loc­al/bin/heif-enc instead of just heif-enc

/*****************************\
* png اور jpg کو avif میں تبدیل کریں *
\*****************************/
add_filter( 'wp_generate_attachment_metadata', 'jps_compress_img', 10, 2 );
تقریب jps_compress_img( $میٹا ڈیٹا, $منسلک_اید )
{
   // get filepath from id
   $filepath= get_attached_file($منسلک_اید);

   // is the file a png or jpeg
   if(pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="JPG" || pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="jpeg" || pathinfo($فائل پاتھ, PATHINFO_EXTENSION)=="PNG")
   {
      // اگر ایسا ہے, run compression of the main file to avif
      shell_exec("/usr / مقامی / بن / heif-enc $ filepath -o $ filepath.avif -q 50 -ایک");
      $حصوں = preg_split('~ /(?=[^ /]*$)', $فائل پاتھ);

      // Then run compression of the thumbnails to avif
      $thumbpaths = $metadata['سائز'];
      ہر ایک کے لئے ($thumbpaths as $key => $انگوٹھے)
      {
          $thumbpath = $ انگوٹھا['فائل'];
          $thumbfullpath = $ حصے[0] . "/" . $انگوٹھے;
          شیل_کسیک("/usr / مقامی / بن / heif-enc $ thumbfullpath -o $ thumbfullpath.avif -q 50 -ایک");
      }
   }
   // give back what we got
   return $metadata;
}

Leave a Reply