2016-04-04 6 views
1

Ich habe add_image_size in functions.php hinzugefügt und die Größe der Bilder angepasst, wenn das Bild groß genug ist, aber wenn das Bild kleiner als die Größe ist, wird das Bild nicht verkleinert.wie add_image_size mit kleineren Bildern arbeiten?

Zum Beispiel habe ich add_image_size('category', 255, 150, true); hinzugefügt. Aber Benutzer hochladen einige Bilder mit der Größe 150x150, es ist nicht groß genug.

Das ist mein Code für Resize Bild, wenn es kleiner als die Größe ich brauche:

if (!function_exists('mit_resize')) { 
    function mit_resize($attachment, $width, $height, $url = false, $crop = true) { 

    $siteurl = get_option('siteurl'); 

    if ($url === false) { 
     $file_path = get_attached_file($attachment); 
    } else if ($attachment) { 
     $file_path = get_attached_file(mit_get_attachment_id($attachment)); 
    } 
    if (empty($file_path)) 
     return false; 

    $file_info = pathinfo($file_path); 
    $extension = '.'. $file_info['extension']; 
    $base_file = $file_info['dirname'].'/'.$file_info['filename'].$extension; 

    $sizes = mit_get_image_sizes(); 
    $general = getimagesize($base_file); 
    foreach ($sizes as $key => $size) { 
     if($size['width'] == $width && $size['height'] == $height && $general[0] >= $size['width'] && $general[1] >= $size['height']) { 
      $path = explode('wp-content',$file_info['dirname'].'/'.$file_info['filename'].'-'.$width.'x'.$height.$extension); 
      return $siteurl . '/wp-content' . $path[1]; 
     } 

    } 

    if (file_exists($base_file)) 
    { 
     $no_ext_path = $file_info['dirname'].'/'.$file_info['filename']; 

     $img_resize = wp_get_image_editor($no_ext_path.$extension); 
     if (! is_wp_error($img_resize)) { 
      $cropped_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension; 
      $img_resize->resize($width, $height, $crop); 
      $img_resize->save($cropped_img_path); 
      if (preg_match("/wp-content/", $cropped_img_path) != false) { 
       $path = explode('wp-content',$cropped_img_path); 
      } 
      else 
       return 'http://placehold.it/' . $width .'x' . $height . '?text=Img'; 
      $url_file = $siteurl . '/wp-content' .$path[1]; 
     } 
    } 

    if (!file_exists($cropped_img_path)) 
    { 
     return 'http://placehold.it/' . $width .'x' . $height . '?text=Img'; 
    } 
    else 
     return $url_file; 
} 

}

Kann ich diese Funktion Haken add_image_size?

+0

ist es nicht möglich. Sie können die Gültigkeit für den Upload min. Größe –

Antwort

0

fand ich eine Antwort für jeden, der das braucht:

if(!function_exists('mit_thumbnail_upscale')) { 
function mit_thumbnail_upscale($default, $orig_w, $orig_h, $new_w, $new_h, $crop){ 

    if (!$crop) return null; // let the wordpress default function handle this 

    $aspect_ratio = $orig_w/$orig_h; 
    $size_ratio = max($new_w/$orig_w, $new_h/$orig_h); 

    $crop_w = round($new_w/$size_ratio); 
    $crop_h = round($new_h/$size_ratio); 

    $s_x = floor(($orig_w - $crop_w)/2); 
    $s_y = floor(($orig_h - $crop_h)/2); 

    return array(0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h); 
} 
} 
add_filter('image_resize_dimensions', 'mit_thumbnail_upscale', 10, 6); 

Nach add_image_size, können Sie diese nur Code in die Funktion setzen. Wenn der Kunde ein Bild hochlädt, das kleiner als Ihre Größe ist, keine Sorge, mit_thumbnail_upscale wird Ihnen helfen.