Asked  7 Months ago    Answers:  5   Viewed   36 times

I am developing a website in PHP that let the user to upload images and then let him to decide how the image should be using jQuery - PHP integeration to select the area that wanted to be the picture and then click the crop button to crop it and save it.

The problem that I am facing is that not all images type are good to crop and save so I noticed that the easy solution for it to convert the image to JPG and then let the user to crop it because it's the easy way to do it in JPG format.

How I can do it?

Is this the best solution for images types problem?

EDIT:

I am using this code to crop images and it's not wroking in PNG format and also limited to 3 ext.

$path_parts = pathinfo("../images/DVDs/".$_POST['logo_file']);
        if ($path_parts['extension'] == "png") {

                $src = imagecreatefrompng("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagepng($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
            } else if ($path_parts['extension'] == "jpg" || $path_parts['extension'] == "jpeg") {

                        $src = imagecreatefromjpeg("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagejpeg($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
                } else if ($path_parts['extension'] == "gif") {

                        $src = imagecreatefromgif("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagegif($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
                    }

I want to convert images to JPG format because it's the easiest to convert without any problem.

 Answers

89

Maybe it's not working with PNG because PNG only supports compression levels 0 to 9.

I'd also rather modify the behaviour based on MIME type, not extension. And I guess you're checking your POST user input before using it in code ;)

Here's my variant of the code:

$path = "../images/DVDs/";

$img = $path . $_POST['logo_file'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");


switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor(350, 494);
imagecopyresampled($tmp, $src, 0, 0, intval($_POST['x']), intval($_POST['y']),
                   350, 494, intval($_POST['w']), intval($_POST['h']));


$thumb = $path . pathinfo($img, PATHINFO_FILENAME) . "_thumb";
switch ($img_info[2]) {
  case IMAGETYPE_GIF  : imagegif($tmp,  $thumb . '.gif');      break;
  case IMAGETYPE_JPEG : imagejpeg($tmp, $thumb . '.jpg', 100); break;
  case IMAGETYPE_PNG  : imagepng($tmp,  $thumb . '.png', 9);   break;
  default : die("Unknown filetype");
}

For every filetype you want supported, you only have to add two lines of code.

Wednesday, March 31, 2021
 
yosemite
answered 7 Months ago
49

The solution was the following:

    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
                $command .= ' -resize "x'.$this->maxHeight.'"';

                // ... and get the middle part of the new image
                // what is the resized width?
                $resized_w = ($this->maxHeight/$height) * $width;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.'+'.round(($resized_w - $this->maxWidth)/2).'+0" +repage';
            } else {
              $command .= ' -resize "' . $this->maxWidth . 'x"';
              $resized_h = ($this->maxWidth/$width) * $height;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                             '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
            }
Saturday, May 29, 2021
 
gMale
answered 5 Months ago
20

There are comparison operators for the DateTime class in php . Something like this :

date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);

Output

bool(false)
bool(true)
bool(false)
Saturday, May 29, 2021
 
RemiX
answered 5 Months ago
33

Using ffmpeg can be helpful. For details http://ffmpeg.org/

Friday, August 6, 2021
 
user505210
answered 3 Months ago
86

After a lot more digging and some help from the guys on the ImageMagick forum I managed to get it working.

The trick to getting it working is the .mpc format. Since this is the native image format used by ImageMagick it does not need to convert the initial image, it just cuts out the piece that it needs. This is the case with the second script I setup.

Lets say you have a 50000x50000 .tif image called myLargeImg.tif. First convert it to the native image format using the following command:

 convert -monitor -limit area 2mb myLargeImg.tif myLargeImg.mpc

Then, run the bellow bash script that will create the tiles. Create a file named tiler.sh in the same folder as the mpc image and put the below script:

 #!/bin/bash
 src=$1
 width=`identify -format %w $src`
 limit=$[$width / 256]
 echo "count = $limit * $limit = "$((limit * limit))" tiles"
 limit=$((limit-1))
 for x in `seq 0 $limit`; do
   for y in `seq 0 $limit`; do
     tile=tile-$x-$y.png
     echo -n $tile
     w=$((x * 256))
     h=$((y * 256))
     convert -debug cache -monitor $src -crop 256x256+$w+$h $tile
   done
 done

In your console/terminal run the below command and watch the tiles appear one at at time into your folder.

 sh ./tiler.sh myLargeImg.mpc
Tuesday, August 10, 2021
 
jonboy
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :