Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UPDATE:</p> <p>If you want to take advantage of Imagick (if it is installed on your server). Note: I didn't use Imagick's nature <code>writeFile</code> because I was having issues with it on my server. File put contents works just as well.</p> <pre><code>&lt;?php /** * * Generate Thumbnail using Imagick class * * @param string $img * @param string $width * @param string $height * @param int $quality * @return boolean on true * @throws Exception * @throws ImagickException */ function generateThumbnail($img, $width, $height, $quality = 90) { if (is_file($img)) { $imagick = new Imagick(realpath($img)); $imagick-&gt;setImageFormat('jpeg'); $imagick-&gt;setImageCompression(Imagick::COMPRESSION_JPEG); $imagick-&gt;setImageCompressionQuality($quality); $imagick-&gt;thumbnailImage($width, $height, false, false); $filename_no_ext = reset(explode('.', $img)); if (file_put_contents($filename_no_ext . '_thumb' . '.jpg', $imagick) === false) { throw new Exception("Could not put contents."); } return true; } else { throw new Exception("No valid image provided with {$img}."); } } // example usage try { generateThumbnail('test.jpg', 100, 50, 65); } catch (ImagickException $e) { echo $e-&gt;getMessage(); } catch (Exception $e) { echo $e-&gt;getMessage(); } ?&gt; </code></pre> <hr> <p>I have been using this, just execute the function after you store the original image and use that location to create the thumbnail. Edit it to your liking... </p> <pre><code>function makeThumbnails($updir, $img, $id) { $thumbnail_width = 134; $thumbnail_height = 189; $thumb_beforeword = "thumb"; $arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name $original_width = $arr_image_details[0]; $original_height = $arr_image_details[1]; if ($original_width &gt; $original_height) { $new_width = $thumbnail_width; $new_height = intval($original_height * $new_width / $original_width); } else { $new_height = $thumbnail_height; $new_width = intval($original_width * $new_height / $original_height); } $dest_x = intval(($thumbnail_width - $new_width) / 2); $dest_y = intval(($thumbnail_height - $new_height) / 2); if ($arr_image_details[2] == IMAGETYPE_GIF) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; } if ($arr_image_details[2] == IMAGETYPE_JPEG) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; } if ($arr_image_details[2] == IMAGETYPE_PNG) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; } if ($imgt) { $old_image = $imgcreatefrom("$updir" . $id . '_' . "$img"); $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height); $imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img"); } } </code></pre> <p>The above function creates images with a uniform thumbnail size. If the image doesn't have the same dimensions as the specified thumbnail size (proportionally), it just has blackspace on the top and bottom.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload