[英]my '.png' image not work for resize image code in php
當我將jpg文件傳遞給imgsize.php?w=100&h=100&img=powered_by.jpg
然后它工作得很好但我將png文件傳遞給imgsize.php?w=100&h=100&img=mengo.png
它不能正常工作
我的imgsize.php文件代碼是
$extension = pathinfo($_GET['img']);
$extension = $extension[extension];
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
header("Content-type: image/jpeg");
}
if ($extension == "png") {
header("Content-type: image/png");
}
if ($extension == "gif") {
header("Content-type: image/gif");
}
$img = $_GET['img'];
$nwidth = $_GET['w'];
$nheight = $_GET['h'];
$img2 = imagecreatefromjpeg($_GET['img']);
$width = imagesx($img2);
$height = imagesy($img2);
$ratio = $width / $height;
$new_nwidth = $nwidth;
$new_nheight = floor($height * ($nwidth / $width));
$im = imagecreatefromjpeg($img) or $im = imagecreatefrompng($img) or $im = imagecreatefromgif($img) or $im = false;
if (!$im) {
} else {
$thumb = imagecreatetruecolor($new_nwidth, $new_nheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $new_nwidth, $new_nheight, $transparent);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $new_nwidth, $new_nheight, $width, $height);
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
imagejpeg($thumb, null, 100);
}
if ($extension == "png") {
imagepng($thumb, null, 9);
}
if ($extension == "gif") {
imagegif($thumb, null, 100);
}
}
任何解決方案嗎? 當我將它傳遞給png文件時,它顯示空白圖像
正如您在if else條件中檢查擴展名生成標題一樣,您必須檢查創建$ img2之類的方式
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG") {
$img2 = imagecreatefromjpeg($_GET['img']);
}
if ($extension == "png") {
$img2 = imagecreatefrompng($_GET['img']);
}
if ($extension == "gif") {
$img2 = imagecreatefromgif($_GET['img']);
}
我要為您的信息添加的另一件事是檢查圖像mime類型而不是檢查擴展名。 為此你可以使用getimagesize函數,它返回一個數組,你可以通過$ retun_array ['mime']檢查mime類型。
如果未正確創建或存儲,PNG圖像通常會產生問題。 損壞的圖像也可能導致此問題。 希望這可能對你有用。
我留下了我的縮略圖圖像代碼並將它們保存到目標文件夾中
function thumbail($forcedwidth, $forcedheight, $sourcefile, $destfile){
$fw = $forcedwidth;
$fh = $forcedheight;
$is = getimagesize( $sourcefile );
$ancho = $is[0];
$alto = $is[1];
if($ancho > $forcedwidth) {
$ancho2 = $forcedwidth;
$por = (100 * $forcedwidth)/$ancho;
$alto2 = ($alto * $por)/100;
$t = 1;
}else{
$ancho2 = $ancho;
$alto2 = $alto;
$t = 2;
}
if($alto > $forcedheight) {
$alto2 = $forcedheight;
$por = (100 * $forcedheight)/$alto;
$ancho2 = ($ancho * $por)/100;
$t = 1;
}else{
$ancho2 = $ancho;
$alto2 = $alto;
}
if ($t == 1){
$img_src = imagecreatefromjpeg( $sourcefile );
$img_dst = imagecreatetruecolor( $ancho2, $alto2 );
imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $ancho2, $alto2, $is[0], $is[1] );
if(!imagejpeg($img_dst, $destfile, 90 )){
exit();
}
}else if ($t == 2){
copy( $sourcefile, $destfile );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.