【问题标题】:Image resize script - multiple sources图像调整大小脚本 - 多个来源
【发布时间】:2013-08-22 08:08:28
【问题描述】:

这里是新手,对编写 php 脚本等非常陌生。

我有一些气象 IP 摄像机可以生成高分辨率 (3MP) 图像,到目前为止,我已经提出了以下脚本来减小图像的大小,并通过 c= "last ip 选择要选择的摄像机八字”。此外,一些相机来自不同的制造商,因此 image.jpg 的路径会有所不同。 一个例子是http://www.example.com/WeatherCam.php?c=2&w=800&h=600,它将从http://10.x.x.2/image.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);
?>  

能否请您帮帮我,以便我可以使用数组或类似方法来选择要使用的相机,而不是我在下面做的粗略方式? 我的想法是这样的:

array(
  'id' => array(
    1 => http://10.x.x.1/image.jpg,
    2 => http://10.x.x.2/image.jpg,
    3 => http://10.x.x.3/jpg/image.jpg
  )

这主要是为了让我可以完全控制我想从 php 脚本访问哪些摄像机,而不是选择同一子网上的任何其他 ip 摄像机。

我希望这样,如果 URL 中未指定 id,它将默认为相机 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);
?> 

【问题讨论】:

    标签: php image get resize


    【解决方案1】:

    我会亲自写下sn-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
    

    不过,这只是偏好问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      相关资源
      最近更新 更多