Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Create thumbnail for display OR Load 50% of image and create image preview

use the following code to create thumb nail images, You can create all type of image file jpeg, png, gif

    $orig_directory = "$desired_dir";  
 //Full image folder
    $thumb_directory = "thumb/";    //Thumbnail

    /* Opening the thumbnail directory and looping
through all the thumbs: */
    $dir_handle = @opendir($orig_directory);
//Open Full image dirrectory
    if ($dir_handle > 1) { //Check to make sure
the folder opened
        $allowed_types = array('jpg', 'jpeg',
'gif', 'png');
        $file_type = array();
        $ext = '';
        $title = '';
        $i = 0;

        while ($file_name = @readdir($dir_handle))
            /* Skipping the system files: */
            if ($file_name == '.' || $file_name ==

            $file_type = explode('.', $file_name);
   //This gets the file name of the images
            $ext =

            /* Using the file name (withouth the
extension) as a image title: */
            $title = implode('.', $file_type);
            $title = htmlspecialchars($title);

            /* If the file extension is allowed:
            if (in_array($ext, $allowed_types)) {

                /* If you would like to inpute
images into a database, do your mysql query here

                /* The code past here is the code
at the start of the tutorial */
                /* Outputting each image: */

                $nw = 100;
                $nh = 100;
                $source =
                $stype = explode(".", $source);
                $stype = $stype[count($stype) -
                $dest = "thumb/{$file_name}";

                $size = getimagesize($source);
                $w = $size[0];
                $h = $size[1];

                switch ($stype) {
                    case 'gif':
                        $simg =
                    case 'jpg':
                        $simg =
                    case 'png':
                        $simg =

                $dimg =
resizePreservingAspectRatio($simg, $nw, $nh);
                imagepng($dimg, $dest);

        /* Closing the directory */

function resizePreservingAspectRatio($img,
$targetWidth, $targetHeight) {
    $srcWidth = imagesx($img);
    $srcHeight = imagesy($img);

    // Determine new width / height preserving
aspect ratio
    $srcRatio = $srcWidth / $srcHeight;
    $targetRatio = $targetWidth / $targetHeight;
    if (($srcWidth <= $targetWidth) &&
($srcHeight <= $targetHeight)) {
        $imgTargetWidth = $srcWidth;
        $imgTargetHeight = $srcHeight;
    } else if ($targetRatio > $srcRatio) {
        $imgTargetWidth = (int) ($targetHeight *
        $imgTargetHeight = $targetHeight;
    } else {
        $imgTargetWidth = $targetWidth;
        $imgTargetHeight = (int) ($targetWidth /

    // Creating new image with desired size
    $targetImg =
imagecreatetruecolor($targetWidth, $targetHeight);

    // Add transparency if your reduced image does
not fit with the new size
    $targetTransparent =
imagecolorallocate($targetImg, 255, 0, 255);
    imagefill($targetImg, 0, 0,

    // Copies image, centered to the new one (if
it does not fit to it)
    imagecopyresampled($targetImg, $img, 0, 0, 0,
0, $targetWidth, $targetHeight, $srcWidth,

    return $targetImg;


Categories : PHP

Related to : Create thumbnail for display OR Load 50% of image and create image preview
Why does the thumbnail I create have a larger file size than the original image?
The thumbnail you create has 4 times the bit depth of the original. Reducing the bit depth will reduce the file size. Edit: To reduce the bit-depth is quite simple, but I can't see any way to do this via CodeIgniter: $im = imagecreatefrompng('./original.png'); imagetruecolortopalette($im, false, 256); imagepng($im, './output.png'); However, this file is still larger than the original (~

Categories : PHP
How to create a button which is always directly below an image when the image's frame changes size
simply give y position of your button to a variable with respect to the y position of your imageView. Try this [button setFrame:CGRectMake(124.0, imageView.frame.size.height + imageView.frame.origin.y + 20, 72.0, 37.0)]; +20 is provided for some space between your imageView and button. Hope this helps :)

Categories : IOS
To Create udf in pig for image processing
Please check if the Apache Tika jar is registered in your Pig script. Ensure that it is available during the execution of the Pig script. ex: REGISTER '/home/user/pig/udfrepository/projectUDF.jar' REGISTER '/home/user/thridpartyjars/xyz.jar';

Categories : Java
Create square 1:1 thumbnail in PHP
Use this code this code uploads image to folder and renames the file and thumb will be created with same name HTML <INPUT NAME="userfile[]" TYPE="file"> image directory "upimg/" thumb directory thimg php processing $rename = md5(rand() * time()); $add = "upimg/" . $rename . $_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $add)) {

Categories : PHP
How To Create Anchors within an image gallery?
You need to append the name or the ID of the image to show to the URL (for example gallery.html?img=img1). You can then use JavaScript in your image gallery code to read this parameter and switch to the image with this name/id.

Categories : Javascript
Recently Add
array_slice, implode, explode, count, for just a string permutation?
How to include a php file from parentdomain?
PHP / MySQL Order By Date & Random
Date and time store error in mysql and php
sorting a numeric php array
phpmailer debug output to html variable
Installing PHP YAML Extension with XAMPP on Windows
how insert data from android to wamp phpmyadmin?
wordpress - Big post IDs, is it Okay in the future?
PHP Change Array Order for Parent-Child Relation
Order by custom metabox value for date in get_posts
error rendering a sitemap with laravel
mysqli INSERTO query blank rows
Laravel error with 404 page
my custom function not working
PHP built-in server error log location
PHP: Appending a class to an include for specific pages
Check array Key Value in included file in PHP
Php Shuffling and inside a Php
PHP pass by reference
Get Facebook Photos without access token
PHP + MySQL No output of data after space in text input value
Parse XML using PHP with multiple namespaces
How do I call SOAP methods via PHP?
Laravel :: Routes Vs. Controller
Twitter Bootstrap navigation tab with does not work
Save content of table to a variable
Link a PHP file to an AJAX request in Magento
Why does my redirect works randomly
php curl_exec returns empty
© Copyright 2017 Publishing Limited. All rights reserved.