Zahir Alam
Zahir Alam

Reputation: 13

php read and write an image in url

I would like to resize an image in URL such as localhost/changimage.php?percent=40. I have already resized it in php. But user should resize it by typing in URL here is my PHP code: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);

 ?>

I have no idea how to do it in URL.

Upvotes: 0

Views: 251

Answers (3)

Dieepak
Dieepak

Reputation: 546

Try it.

$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);

Upvotes: 1

zyntrax
zyntrax

Reputation: 136

OR

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

To check if percent has a value

Upvotes: 1

Amir
Amir

Reputation: 4111

maybe something like that:

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

Upvotes: 0

Related Questions