2016-01-12 46 views
1

我想重新調整上傳圖片的大小並移動到一個文件夾中,我嘗試了打擊代碼,但它不工作。我創建了一個調整照片大小的功能,從另一個功能調用該功能,但是圖像的大小不重,照片不會移動到文件夾。如何調整上傳圖片的大小和移動到一個文件夾

$final_save_dir = 'techpic'; 
$thumbname = $_FILES['tpic']['name']; 
$imgName = $final_save_dir . '/' . $_FILES['tpic']['name']; 
if(@move_uploaded_file($_FILES['tpic']['tmp_name'], $final_save_dir . '/' . $_FILES['tpic']['name'])) 
{ 
$this->createThumbnail($thumbname,"600","600",$final_save_dir,$final_save_dir); 
} 
function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir) 

{ $ path = $ uploadDir。 '/'。 $ IMAGE_NAME;

$mime = getimagesize($path); 

    if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); } 
    if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); } 
    if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); } 
    if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); } 

    $old_x   = imageSX($src_img); 
    $old_y   = imageSY($src_img); 

    if($old_x > $old_y) 
    { 
     $thumb_w = $new_width; 
     $thumb_h = $old_y*($new_height/$old_x); 
    } 

    if($old_x < $old_y) 
    { 
     $thumb_w = $old_x*($new_width/$old_y); 
     $thumb_h = $new_height; 
    } 

    if($old_x == $old_y) 
    { 
     $thumb_w = $new_width; 
     $thumb_h = $new_height; 
    } 

    $dst_img  = ImageCreateTrueColor($thumb_w,$thumb_h); 

    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 


    // New save location 
    $new_thumb_loc = $moveToDir . $image_name; 

    if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); } 
    if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } 
    if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } 
    if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } 

    imagedestroy($dst_img); 
    imagedestroy($src_img); 

    return $result; 

}

+0

當試圖調試問題時,抑制錯誤報告並不是最好的主意(不是它是個好主意):'@ move_uploaded_file' – CD001

回答

1

在PHP調整圖像大小,你可以使用Imagick::resizeImagethis article或使用this article

我們當然可以使用具有通過@ CD001的建議,低開銷的GD庫。你可以找到GD的解釋在this article

編輯重新上漿更多的解釋

使用Imagick::resizeImage你應該可以看到這個說明

這裏是函數的原型:

bool Imagick::resizeImage (int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ]) 

參數

columns 
Width of the image 

rows 
Height of the image 

filter 
Refer to the list of filter constants. 

blur 
The blur factor where > 1 is blurry, < 1 is sharp. 

bestfit 
Optional fit parameter. 

如果你想在這裏使用GD庫是簡單的源代碼

<?php 
// File and new size 
//the original image has 800x600 
$filename = 'images/picture.jpg'; 
//the resize will be a percent of the original size 
$percent = 0.5; 

// Content type 
header('Content-Type: image/jpeg'); 
// Get new sizes 
list($width, $height) = getimagesize($filename); 
$newwidth = $width * $percent; 
$newheight = $height * $percent; 
// Load 
$thumb = imagecreatetruecolor($newwidth, $newheight); 
$source = imagecreatefromjpeg($filename); 
// Resize 
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

// Output and free memory 
//the resized image will be 400x300 
imagejpeg($thumb); 
imagedestroy($thumb); 
+0

GD庫在服務器上的開銷較低......只是說。 – CD001

+0

是的你是對的@ CD001 –

+0

這應該是一個評論而不是答案。在這裏糾正他/她的代碼沒有任何關係。 – RST

1
function img($field = 'image', $width = null, $height = null, $crop = false, $alt = null, $turl = null) { 
global $editormode; 

$val = $field; 

if (!$val) 
    $val = 'no-image.png'; 


$alt = ($alt) ? $alt : stem(basename($val)); 

if ($width == null && $height == null) 
    $imgf = get_dir() . $val; 
else 
    $imgf = gen_img($val, $width, $height, $crop); 

if (!$imgf) 
    return ""; 
$url = $imgf; 

if (!$turl) 
    return "<img src='$url' alt='$alt'/>\n"; 
else 
    return "<a href='$turl'><img src='$url' alt='$alt'/></a>"; 
    } 

function get_dir() { 
return "upload/"; 
} 
function gen_img($fileval, $width, $height, $crop) { 
if (!$fileval) 
    return null; 
$fname = get_dir() . $fileval; 

if (!is_readable($fname)) 
    return null; 

$stem = stem(basename($fname)); 

if ($width != null && $height != null) { 
    $sz = getimagesize($fname); 
    if ($sz[0] == $width && $sz[1] == $height) { 
     return substr($fname, strlen(UPLOADROOT)); 
    } 
    $sep = ($crop) ? '__' : '_'; 

    $outname = thumb_dir($fname) . $stem . $sep . $width . "x" . $height . "." . suffix($fname); 

    if (!is_readable($outname) || filemtime($outname) < filemtime($fname)) 
     createthumb($fname, $outname, $width, $height, $crop); 
} 
else if ($width != null && $height == null) { 
    $outname = thumb_dir($fname) . $stem . "_" . $width . "." . suffix($fname); 
    if (!is_readable($outname) || filemtime($outname) < filemtime($fname)) 
     createthumb($fname, $outname, $width, $crop); 
} else 
    $outname = $fname; 
//echo $outname; die(); 
return $outname; 
} 
function thumb_dir($path) { 
$enddir = strrpos($path, "/"); 
$dir = substr($path, 0, $enddir) . "/.thumbnails/"; 
if (!file_exists($dir)) 
    mkdir($dir, 0777, true); 
return $dir; 
} 
function createthumb($source, $dest, $new_w, $new_h = null, $crop = false) { 
if (!file_exists($source)) 
    return null; 

$src_img = 0; 
$src_img = image_create($source); 
$old_w = imageSX($src_img); 
$old_h = imageSY($src_img); 
$x = $y = 0; 

if ($new_h == null) { // we want a square thumb, cropped if necessary 
    if ($old_w > $old_h) { 
     $x = ceil(($old_w - $old_h)/2); 
     $old_w = $old_h; 
    } else if ($old_h > $old_w) { 
     $y = ceil(($old_h - $old_w)/2); 
     $old_h = $old_w; 
    } 
    $thumb_w = $thumb_h = $new_w; 
} else if ($crop) { 
    $thumb_w = $new_w; 
    $thumb_h = $new_h; 
    $oar = $old_w/$old_h; 
    $nar = $new_w/$new_h; 
    if ($oar < $nar) { 
     $y = ($old_h - $old_h * $oar/$nar)/2; 
     $old_h = ($old_h * $oar/$nar); 
    } else { 
     $x = ($old_w - $old_w * $nar/$oar)/2; 
     $old_w = ($old_w * $nar/$oar); 
    } 
} else if ($new_w * $old_h/$old_w <= $new_h) { // retain aspect ratio, limit by new_w 
    $thumb_h = $new_w * $old_h/$old_w; 
    $thumb_w = $new_w; 
} else { // retain aspect ratio, limit by new_h 
    $thumb_w = $new_h * $old_w/$old_h; 
    $thumb_h = $new_h; 
} 

$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); 

imagecolortransparent($dst_img, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); 
imagealphablending($dst_img, false); 
imagesavealpha($dst_img, true); 

imagecopyresampled($dst_img, $src_img, 0, 0, $x, $y, $thumb_w, $thumb_h, $old_w, $old_h); 

image_save($dst_img, $dest); 

imagedestroy($dst_img); 
imagedestroy($src_img); 
} 
function image_create($source) { 
$suf = strtolower(suffix($source)); 
if ($source == '.jpg') 
    mylog("wtf", "source: $source", true); 

if ($suf == "png") 
    return imagecreatefrompng($source); 
else if ($suf == "jpg" || $suf == "jpeg") 
    return imagecreatefromjpeg($source); 
else if ($suf == "gif") 
    return imagecreatefromgif($source); 
return null; 
} 

function image_save($dst_img, $dest) { 
$suf = strtolower(suffix($dest)); 
if ($suf == "png") 
    imagepng($dst_img, $dest); 
else if ($suf == "jpg" || $suf == "jpeg") 
    imagejpeg($dst_img, $dest); 
else if ($suf == "gif") 
    imagegif($dst_img, $dest); 
} 

這是你函數放在你的函數文件

該函數使文件夾在調用函數文件時,位於圖像文件夾中的縮略圖中。

以下方式在圖像顯示時調用該函數。

<?php echo img('pages/sample_image.jpg', 122, 81, TRUE) ?> 

這裏第一個是圖像的路徑和122:平均寬度和81:高度和真/假真:裁剪圖像和假:只調整圖像的大小。

並在你的配置文件中定義Uploadroot這個路徑的圖像文件夾。

希望這個工程。

謝謝。

相關問題