php在url中读取并写入图像

时间:2013-03-26 08:01:11

标签: php

我想在localhost/changimage.php?percent=40等网址中调整图片大小。我已经在php中调整了它的大小。但是用户应该通过输入URL来调整它的大小 这是我的PHP代码:changimage.php

<?php

 $filename = '/var/www/html/zahir/images/bike.jpeg';

  $percent = 0.5; 

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

    list($width, $height) = getimagesize($filename);

     $new_width = $width * $percent;

       $new_height = $height * $percent;

         $image_p = imagecreatetruecolor($new_width, $new_height);

  $image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

  imagejpeg($image_p, null, 100);

 ?>

我不知道如何在网址中执行此操作。

3 个答案:

答案 0 :(得分:1)

试试吧。

$percent = 0;
   if (isset($_GET["percent"])) {
       $percent = ($_GET["percent"] / 100);
      }
    if ($percent <= 0) {
        $percent = 1;
    }

    $filename = '/home/deepakgupta/Pictures/phone.jpg';

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

    list($width, $height) = getimagesize($filename);

    $new_width = $width * $percent;

    $new_height = $height * $percent;

    $image_p = imagecreatetruecolor($new_width, $new_height);

    $image = imagecreatefromjpeg($filename)
    ;
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    imagejpeg($image_p, null, 100);

答案 1 :(得分:1)

OR

$percent = 0.5;
if (isset($_GET['percent'])) {$percent = $_GET['percent']/100;}

检查百分比是否有值

答案 2 :(得分:0)

也许是这样的:

$percent = 0.5;
if ($_GET['percent']) {$percent = $_GET['percent']/100;}