Category Archives: Uncategorized

Tools ‹ phpspecialist — WordPress

Tools ‹ phpspecialist — WordPress.


When viewing files online, it is often necessary (or at least convenient) to be able to create a thumbnail image on the fly. You may want to create previews of photos you are linking to, or perhaps resize photos for your website to a more user friendly size without having to bother with a graphics program.

In this tutorial we will explore how to do this using imagecopyresized () and using imagecopyresampled. We will also cover calling the image from the PHP file, as well as saving the image to your server.

1. Using Imagecopyresized ():-

When using imagecopyresized () we have a number of parameters that need to be entered. They are: destination_image, source_image, starting_X_ordinate, starting_Y_ordinate, initial_X_image_startpoint, initial_Y_image_startpoint, width, height, original_width, original_height.

Here is an example of how to resize an image to a certain % of it’s original size, and keep it proportional. Pay attention to the comments in the code, as they explain what we are doing in each step.

<?php
// The file you are resizing
$file = ‘your.jpg’;

//This will set our output to 45% of the original size
$size = 0.45;

// This sets it to a .jpg, but you can change this to png or gif
header(‘Content-type: image/jpeg’);

// Setting the resize parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;

// Creating the Canvas
$tn= imagecreatetruecolor($modwidth, $modheight);
$source = imagecreatefromjpeg($file);

// Resizing our image to fit the canvas
imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputs a jpg image, you could change this to gif or png if needed
imagejpeg($tn);
?>

2.Using Imagecopyresampled ():-

If you need a good quality reduction, and imagecopyresized () doesn’t look nice enough, you may have better luck with imagecopyresampled (). The parameters you need to define in imagecopyresampled () are: destination_image, source_image, starting_X_ordinate, starting_Y_ordinate, initial_X_image_startpoint, initial_Y_image_startpoint, width, height, original_width, original_height.

Here is an example of how to use imagecopyresampled (). If you are having trouble understanding, be sure to pay attention to the comments in the code, they explain everything that is going on in each step.

<?php
// The file you are resizing
$file = ‘yourfile.jpg’;

//This will set our output to 45% of the original size
$size = 0.45;

// This sets it to a .jpg, but you can change this to png or gif
header(‘Content-type: image/jpeg’);

// Setting the resize parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
?>


Advanced Techniques

Now I know that PHP isn’t very “cool” these days; it’s probably the most misunderstood web language because all the script kiddies use it to include files on their webpages and put up forms that get hacked hours later. But I think PHP deserves a whole lot more credit than it gets; it’s fast, it integrates very nicely with Apache, it got all the right features in version 5, and even if you don’t like using it, there’s always a chance you might have to write some anyway. So, maybe these tips can help you out:

  • Use heredoc syntax for strings: If there’s one thing I got tired of fast, it was building long strings like this:
    $s = "This is something ";
    $s .= "that will make ";
    $s .= "a long string, with ";
    $s .= "a variable: $x.";

    If I am going to make a string with more than one line, and even sometimes when I’m not, then I use heredoc. It’s much, much cleaner than worrying about quotation marks, because it isn’t delimited with them. Here’s the same string as above, in one clean heredoc block:

    $s = <<<HEREDOC
      This is something 
      that will make 
      a long string, with 
      a variable: {$x}.
    HEREDOC;

    And, that delimiter can be anything; EOF, MYSTRING, ELEPHANT, etc. The only caveat is that the ending delimiter has to be all the way at the left; you can’t put any spaces or tabs before it. This is a slight nuisance if you are serious about maintaining indentation, but it’s a small compromise for having such a clean way of making strings.

  • Contain your variables in strings with curly braces. This goes along with the heredoc syntax above, but you can use it in double-quote delimited strings too. It looks like so:
    $x = "Something with {$y['key']} and {$z}.";

    It allows you to include array variables in strings and it makes the variables stand out better in most any text editor. Update 11/12: I should mention, for anyone who doesn’t know, that using curly braces in your strings is not recommended when you are working with user input. See this bug. Either validate the user input to make sure that there are no curly braces before plugging it in, or don’t use curly braces with user input at all.

  • Build arrays. Always. You probably won’t make just one of something. Sooner or later, you’ll realize you need two, and then you’ll have to go back and turn that variable into an array so you can store multiple things. I walked into this mistake too many times… I would be processing a form, and have a variable called $error to store a possible error, and then I realize 10 minutes later that there might be more than one error, and I need to make an array of errors, and I have to go back and rewrite things to use an array instead. Nowadays I don’t get into that mess anymore; I just start with arrays and save myself the trouble.
  • Use associative arrays. (These are called hashes too.) They make things easier to keep track of. This goes along with the previous tip, though if you are just making a linear list of things, there’s no need. Where associative arrays come in handy is when you need to remember the keys you use, because you can refer to the key with a variable. Say I have an array that I use to retrieve the address for a page, I can do the following (this is a horrible example though):
    $pages = array(
     'index' => 'startPage.php',
     'contact' => 'sendForm.php'
    );
     
    // could do some function here
    $thePage = 'contact';
     
    $theAddress = $pages[$thePage];
     
    // here I'll get sendForm.php

    I think that’s very convenient.


PHP PROXY?

A PHP proxy is a single script or group of scripts that takes information from a web page or interface and transfers it to a web server for some type of processing. PHP proxies can be simple or complex, taking input from a single form or linking together multiple interfaces and computer terminals into a coherent whole. Unlike the client-side JavaScript language, PHP scripts and proxies are all run on a web server, which gives them the advantage of not needing to rely on software a user has installed.

Scripts comprising a PHP proxy can be written in two ways. First, they can be written as a sequence of commands, to be executed one after the other. Second, they can be structured like classes in an object-oriented programming language. Either type of PHP script can be included inside other PHP scripts to minimize repeated code throughout the proxy.


What is the GD Library?

The GD library is used for dynamic image creation. From PHP we use with the GD library to create GIF, PNG or JPG images instantly from our code. This allows us to do things such as create charts on the fly, created an an anti-robot security image, create thumbnail images, or even build images from other images.

If you are unsure if you have GD library, you can run phpinfo() to check that GD Support is enabled.


GD Library

The GD Library is used to create and manipulate graphics via PHP. You can create an image from scratch, create thumbnails for your photos, add text to photos, resize images, recolor images, and more


Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!