Php 图像大小调整脚本-多个源

Php 图像大小调整脚本-多个源,php,image,get,resize,Php,Image,Get,Resize,这里很新,对编写php脚本也很陌生 我有一些天气IP摄像机,它们可以生成高分辨率(3MP)图像,到目前为止,我已经提出了以下脚本,以减小图像的大小,并通过c=“last IP octet”选择要选择的摄像机。此外,一些相机来自不同的制造商,因此图像.jpg的路径将不同。 例如,它将从(摄影机2)中提取图像,然后将其减少到800x600像素 <?php // Get Camera Number if(isset($_GET['c'])){ $num=$_GET['c']; } /

这里很新,对编写php脚本也很陌生

我有一些天气IP摄像机,它们可以生成高分辨率(3MP)图像,到目前为止,我已经提出了以下脚本,以减小图像的大小,并通过c=“last IP octet”选择要选择的摄像机。此外,一些相机来自不同的制造商,因此图像.jpg的路径将不同。 例如,它将从(摄影机2)中提取图像,然后将其减少到800x600像素

<?php  
// Get Camera Number
 if(isset($_GET['c'])){
  $num=$_GET['c'];
}

// The image from camera
$filename = "http://10.99.99.".$num."/image.jpg"; 

// Set a maximum height and width  
$width = 2048;  
$height = 1536;  

 if(isset($_GET['w'])){
  $width=$_GET['w'];
}
 if(isset($_GET['h'])){
   $height=$_GET['h'];
}

// Set Content type to jpeg 
header('Content-type: image/jpeg');  

list($width_orig, $height_orig) = getimagesize($filename);  
$ratio_orig = $width_orig/$height_orig;  

if ($width/$height > $ratio_orig) {  
   $width = $height*$ratio_orig;  
} else {  
   $height = $width/$ratio_orig;  
}  

// Create new images  
$image_p = imagecreatetruecolor($width, $height);
$sourceimage = imagecreatefromjpeg($filename);  
imagecopyresampled($image_p, $sourceimage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);  

// Output Images & Cleanup
imagejpeg($image_p, null, 100);
imagedestroy($sourceimage);
imagedestroy($image_p);
?>  
这主要是为了让我能够完全控制希望从php脚本访问哪些摄像头,而不选择同一子网上的任何其他ip摄像头

我希望如此,如果URL中未指定id,它将默认为camera 1或其他图像,即imagenotfound.jpg

编辑: 下面是我提出的新代码,任何人都能看到任何问题吗?到目前为止,它似乎运作良好

<?php  
// Get Filename from URL  
if (isset($_GET['ID']) AND $_GET['ID'] == 'null') {
    $filename = 'no-image.jpg';
}
switch ((isset($_GET['ID']) ? $_GET['ID'] : '')) {
    case '1':
        $filename = "http://10.x.x.1/jpg/image.jpg";
        break;
    case '2':
        $filename = "http://10.x.x.2/jpg/image.jpg";
        break;
    case '3':
        $filename = "http://10.x.x.1/image.jpg";
        break;
    default:
        $filename = 'no-image.jpg';
        break;
}

// Get Image Quality from URL  
if (isset($_GET['q']) AND $_GET['q'] == 'null') {
    $quality = '100';
}
switch ((isset($_GET['q']) ? $_GET['q'] : '')) {
    case '1':
        $quality = "100";
        break;
    case '2':
        $quality = "80";
        break;
    case '3':
        $quality = "75";
        break;
    default:
        $quality = '100';
        break;
}

// Get Image Width & Height from URL  
if (isset($_GET['s']) AND $_GET['s'] == 'null') {
        $width = "800";
        $height = "600";
}
switch ((isset($_GET['s']) ? $_GET['s'] : '')) {
    case '1':
        $width = "800";
        $height = "600";
        break;
    case '2':
        $width = "1024";
        $height = "768";
        break;
    case '3':
        $width = "704";
        $height = "576";
        break;
    case '4':
        $width = "2048";
        $height = "1536";
        break;
    default:
        $width = "800";
        $height = "600";
        break;
}

// Content type  
header('Content-type: image/jpeg');  

// Get new dimensions  
list($width_orig, $height_orig) = getimagesize($filename);  

$ratio_orig = $width_orig/$height_orig;  

if ($width/$height > $ratio_orig) {  
   $width = $height*$ratio_orig;  
} else {  
   $height = $width/$ratio_orig;  
}  

// Resample  
$image_p = imagecreatetruecolor($width, $height);
$sourceimage = imagecreatefromjpeg($filename);  
imagecopyresampled($image_p, $sourceimage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);  

// Output  
imagejpeg($image_p, null, $quality);
imagedestroy($sourceimage);
imagedestroy($image_p);
?> 

我将亲自编写以下代码片段:

// Get Image Quality from URL  
if (isset($_GET['q']) AND $_GET['q'] == 'null') {
    $quality = '100';
}
switch ((isset($_GET['q']) ? $_GET['q'] : '')) {
    case '1':
        $quality = "100";
        break;
    case '2':
        $quality = "80";
        break;
    case '3':
        $quality = "75";
        break;
    default:
        $quality = '100';
        break;
}
这样:

$quality_list = array(
    1 => 100,
    2 => 80,
    3 => 75
);

$quality = (isset($_GET['q']) && isset($quality_list[$_GET['q']]))  
       ? $quality_list[$_GET['q']]
       : 100;  // default quality
然而,这只是偏好的问题

$quality_list = array(
    1 => 100,
    2 => 80,
    3 => 75
);

$quality = (isset($_GET['q']) && isset($quality_list[$_GET['q']]))  
       ? $quality_list[$_GET['q']]
       : 100;  // default quality