2014-02-11 74 views
-1

我需要將圖片大小調整爲固定大小。但它不工作,並有錯誤,我該怎麼辦?用PHP調整圖片大小?

在線演示:http://codepad.org/3OrIHfoy

錯誤:

Fatal error: Call to undefined function imagecreatefromjpeg() on line 58

PHP:

<?php 
function thumbnail_box($img, $box_w, $box_h) { 
    //create the image, of the required size 
    $new = imagecreatetruecolor($box_w, $box_h); 
    if($new === false) { 
     //creation failed -- probably not enough memory 
     return null; 
    } 


    //Fill the image with a light grey color 
    //(this will be visible in the padding around the image, 
    //if the aspect ratios of the image and the thumbnail do not match) 
    //Replace this with any color you want, or comment it out for black. 
    //I used grey for testing =) 
    $fill = imagecolorallocate($new, 200, 200, 205); 
    imagefill($new, 0, 0, $fill); 

    //compute resize ratio 
    $hratio = $box_h/imagesy($img); 
    $wratio = $box_w/imagesx($img); 
    $ratio = min($hratio, $wratio); 

    //if the source is smaller than the thumbnail size, 
    //don't resize -- add a margin instead 
    //(that is, dont magnify images) 
    if($ratio > 1.0) 
     $ratio = 1.0; 

    //compute sizes 
    $sy = floor(imagesy($img) * $ratio); 
    $sx = floor(imagesx($img) * $ratio); 

    //compute margins 
    //Using these margins centers the image in the thumbnail. 
    //If you always want the image to the top left, 
    //set both of these to 0 
    $m_y = floor(($box_h - $sy)/2); 
    $m_x = floor(($box_w - $sx)/2); 

    //Copy the image data, and resample 
    // 
    //If you want a fast and ugly thumbnail, 
    //replace imagecopyresampled with imagecopyresized 
    if(!imagecopyresampled($new, $img, 
     $m_x, $m_y, //dest x, y (margins) 
     0, 0, //src x, y (0,0 means top left) 
     $sx, $sy,//dest w, h (resample to this size (computed above) 
     imagesx($img), imagesy($img)) //src w, h (the full size of the original) 
    ) { 
     //copy failed 
     imagedestroy($new); 
     return null; 
    } 
    //copy successful 
    return $new; 
} 
$i = imagecreatefromjpeg("http://techstroke.com/wp-content/uploads/2008/12/image2.png"); 
$thumb = thumbnail_box($i, 210, 150); 
imagedestroy($i); 

if(is_null($thumb)) { 
    /* image creation or copying failed */ 
    header('HTTP/1.1 500 Internal Server Error'); 
    exit(); 
} 
header('Content-Type: image/jpeg'); 
imagejpeg($thumb); 
+4

我喜歡你試圖使用imagecreatefrom ** jpeg **來加載PNG。美好時光。無論哪種方式,它看起來像你沒有安裝你的發行版的php5-gd軟件包。 –

回答

0

的錯誤......

Fatal error: Call to undefined function imagecreatefromjpeg() on line 58 

imagecreatefromjpeg(),因爲它做的不是調用es不存在。這意味着GD庫尚未安裝在您的系統上。

here are the installation notes。你需要他們讓一切順利。既然你在互聯網上抓取了這個php函數,一旦你安裝了GD庫,它應該可以正常工作。