Question

i've tried to make a image gallery, but I'm stuck on shrinking script. I have it made as function. Original input $_FILES['image'] is substitued on $original.

I'm pretty sure this part is OK, but I'm adding it for better orientation.

$orig_udaje = getimagesize($original['tmp_name']);
    $orig_sirka = $orig_udaje[0];  //original width
    $orig_vyska = $orig_udaje[1];  //original height
    $orig_typ = $orig_udaje[2];    //type of original image

$div_size = 150;  //size of image div = size of future thumbnail's smaller side

// choosing smaller side & defining resizing scale
if ($orig_sirka>$orig_vyska)
    {
      $mira = $orig_vyska / $div_size; 
    }
  else
    {
      $mira = $orig_sirka / $div_size;
    }

  $nahled_sirka = $orig_sirka / $mira;
  $nahled_vyska = $orig_vyska / $mira;

I think the troubled part is somewhere below this text.

  $nahled_cache = imagecreatetruecolor($nahled_sirka, $nahled_vyska);
  imagecolortransparent($nahled_cache, "0, 0, 0");

  if ($orig_typ == "image/jpeg")
    {
      $nahled_tvorba = imagecreatefromjpeg($original['tmp_name']);      
      imagecopyresampled($nahled_cache, $nahled_tvorba, 0, 0, 0, 0, $nahled_sirka, $nahled_vyska, $orig_sirka, $orig_vyska);

      imagejpeg($nahled_cache, "/data/images/gallery/thumbs/output.jpg");
      imagejpeg($original['tmp_name'], "/data/images/gallery/originals/output.jpg");
      unlink($original['tmp_name']);
    }
  if ($orig_typ == "image/png")
    {
      $obrazek_tvorba = imagecreatefrompng($original['tmp_name']);
      imagecopyresampled($nahled_cache, $nahled_tvorba, 0, 0, 0, 0, $nahled_sirka, $nahled_vyska, $orig_sirka, $orig_vyska);

      imagepng($nahled_cache, "/data/images/gallery/thumbs/output.png");
      imagepng($original['tmp_name'], "/data/images/gallery/originals/output.png");
      unlink($original['tmp_name']);                
    }  

Thank you for your advice.

Was it helpful?

Solution

I suggest you not to write the code, that already written so many times... Try to use some open source classes. Like this one. The class is easy and lightweight, and have pretty good documentation. The usage is simple:

    // *** Include the class
    include("resize-class.php");

    // *** 1) Initialize / load image
    $resizeObj = new resize('sample.jpg');

    // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
    $resizeObj -> resizeImage(150, 100, 'crop');

    // *** 3) Save image
    $resizeObj -> saveImage('sample-resized.gif', 100);

You can use 'auto' option instead of 'crop', and provide max width and height. This mean's, that the image resolution will be generated based on the maximum properties you provided, and also, based on image orientation. Try it ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top