Nasıl bu PHP kodu artırabilir?

6 Cevap

Bana bir komut bir fotoğrafın küçük resim yolunu almak yardımcı olan aşağıdaki php kodu var

It will take a supplied value like this from a mysql DB '2/34/12/thepicture.jpg' It will then turn it into this '2/34/12/thepicture_thumb1.jpg'

Eminim bunu yapmanın daha iyi bir performans yolu var ve ben her türlü yardıma açığım lütfen

Ayrıca 50 kullanıcının bir sayfada bu 50 farklı fotoğrafları almak için 50 kez aday olacağını

// the photo has it is pulled from the DB, it has the folders and filename as 1
$photo_url = '2/34/12/thepicture_thumb1.jpg';
//build the full photo filepath
$file = $site_path. 'images/userphoto/' . $photo_url;
// make sure file name is not empty and the file exist 
if ($photo_url != '' && file_exists($file)) {
    //get file info
    $fil_ext1 = pathinfo($file);
    $fil_ext = $fil_ext1['extension'];
    $fil_explode = '.' . $fil_ext;
    $arr = explode($fil_explode, $photo_url);
    // add "_thumb" or else "_thumb1" inbetween 
    // the file name and the file extension 2/45/12/photo.jpg becomes 2/45/12/photo_thumb1.jpg
    $pic1 = $arr[0] . "_thumb" . $fil_explode;
    //make sure the thumbnail image exist
    if (file_exists("images/userphoto/" . $pic1)) {
    	//retunr the thumbnail image url
    	$img_name = $pic1;
    }
}

Ben merak ediyorum 1 şey uzantısı her zaman 3 basamaklı olacak çünkü, dosya uzantısı almak için () pathinfo kullanır nasıl, olur bu değer daha iyi performans almanın diğer yöntemler?

6 Cevap

Performans açısından, aradığınız eğer yerleşik PHP fonksiyonları size perde arkasında derlenmiş kod çalıştıran çünkü performansı mükemmel.

Tabii ki, gerek yok, tüm bu işlevleri çağırarak iyi bir fikir değildir. Senin durumunda, pathinfo function ihtiyacınız çeşitli yolları döndürür. Eğer böyle dosya adını inşa edebilirsiniz zaman orijinal adı explode işlevini çağırın (not, 'filename' PHP 5.2 beri kullanılabilir):

$fInfo = pathinfo($file);
$thumb_name = $fInfo['dirname'] . '/' . $fInfo['filename'] . '_thumb' . $fInfo['extension'];

PHP 5.2 yoksa, o zaman basit yolu bu işlevi göz ardı ve strrpos ve substr kullanmaktır:

// gets the position of the last dot
$lastDot = strrpos($file, '.');
// first bit gets everything before the dot,
// second gets everything from the dot onwards
$thumbName = substr($file, 0, $lastDot) . '_thumb1' . substr($file, $lastDot);

Bu kod ile bir performans sorunu var mı, yoksa sadece erken optimize edilir? Performans kullanılabilirlik konu olacak kadar kötü ve profiler bu kod sorumlu olduğunu söylemediği sürece, bu kod ile acil sorunlar çok daha fazlası vardır.

Soruyu yanıtlamak için: "Nasıl bu PHP kodu artırabilir" Add whitespace.

Bu kod için en iyi optimizasyonu okunabilirliği artırmak için:

// make sure file name is not empty and the file exist 
if ( $photo_url != '' && file_exists($file) ) {

    // Get information about the file path
    $path_info = pathinfo($file);

    // determine the thumbnail name 
    // add "_thumb" or else "_thumb1" inbetween 
    // the file name and the file extension 2/45/12/photo.jpg
    // becomes 2/45/12/photo_thumb.jpg
    $pic1 = "{$path_info['dirname']}/{$path_info['basename']}_thumb.{$fil_ext}";

    // if this calculated thumbnail file exists, use it in place of
    // the image name
    if ( file_exists( "images/userphoto/" . $pic1 ) ) {
        $img_name = $pic1;
    }
}

Ben satır sonlarını kullanarak fonksiyonun bileşenlerini kırık ve küçük isim belirleme sürecini kolaylaştırmak için pathinfo() döndürülen bilgileri kullandık.

Updated @ DisgruntledGoat geribildirim dahil

Neden bu işlevin performansı hakkında da endişe duyuyorlar? Eğer ("ana" dosya oluşturulduğunda, diyelim ki) sadece bir kez onu aramak ve sonucu depolamak varsayarak, onun zamanı DB ve dosya sistemi erişim karşılaştırıldığında esasen sıfır olmalıdır. Eğer resim yolunu yeniden hesaplamak için her erişim çağırarak iseniz, iyi, savurgan ama yine de önemli ölçüde etkileyen çalışma zamanı olacak değil.

Eğer daha güzel bir görünüm ve daha sürdürülebilir olmasını istiyorsanız Şimdi, o değerli bir hedeftir.

Bunu düzeltmek için en kolay yolu, önce el tüm kullanıcı profili resimlerinizi thumbnail ve boyutlandırma tutmak kalmamak etrafında tutmaktır.

('.? / ^ (*) (\ .. *) $ /', '\ 1_thumb 2 \', $ dosya) {[(0)];}

Düzenleme: bbcode \ kayboldu.