Tag: php

Entries for tag "php", ordered from most recent. Entry count: 4.

Uwaga! Informacje na tej stronie mają ponad 3 lata. Nadal je udostępniam, ale prawdopodobnie nie odzwierciedlają one mojej aktualnej wiedzy ani przekonań.

Pages: 1

20:32
Fri
06
Jan 2012

Resizing Images to Generate Thumbnails in PHP

Some time ago I came across a problem of calculating an image size for automatically generated thumbnail for gallery script coded in PHP. It required a moment's thought, so I'd like to share this algorithm. In fact there will be two algorithms.

First code scales the image down with following rules: Destination size will not exceed given thumbnail size, but the width or height can be smaller to always preserve aspect ratio. Images smaller than thumbnail size are not magnified.

Inputs:
$src_size_x, $src_size_y - Source image size.
THUMBNAIL_SIZE_X, THUMBNAIL_SIZE_Y - Constants defining thumbnail size.

Outputs:
$dst_size_x, $dst_size_y - Destination thumbnail size.

if( $src_size_x <= THUMBNAIL_SIZE_X && $src_size_y <= THUMBNAIL_SIZE_Y )
{
  $dst_size_x = $src_size_x;
  $dst_size_y = $src_size_y;
}
else
{
  $dst_size_x = THUMBNAIL_SIZE_X;
  $dst_size_y = (int)( $src_size_y * THUMBNAIL_SIZE_X / $src_size_x );
  
  if( $dst_size_y > THUMBNAIL_SIZE_Y )
  {
    $dst_size_x = (int)( $src_size_x * THUMBNAIL_SIZE_Y / $src_size_y );
    $dst_size_y = THUMBNAIL_SIZE_Y;
  }
}

Second algorithm also helps with generating image thumbnails, but works differently. It assumes that destination image will always be of size (THUMBNAIL_SIZE_X, THUMBNAIL_SIZE_Y), while source iamge can be cropped to select only the center of the image if it has different aspect ratio than the thumbnail.

Inputs to this algorithm are the same, while outputs are:

$src_x, $src_y - Offset in the source image to begin copying from.
$src_w, $src_h - Size of the rectangle to select from cropped source image.

The code that uses this algorithm should then select from the source image a rectangle with left-top position ($src_x, $src_y), size ($src_w, $src_h) and copy it, with scaling and resampling, to the destination image with the size exactly (THUMBNAIL_SIZE_X, THUMBNAIL_SIZE_Y). That's what imagecopyresampled function from GD library can do. This algorithm does not handle cases where source image is smaller than thumbnail.

$src_h = $src_size_y;
$src_w = (int)( $src_h * THUMBNAIL_SIZE_X / THUMBNAIL_SIZE_Y );

if( $src_w <= $src_size_x )
{
  $src_x = ( $src_size_x - $src_w ) / 2;
  $src_y = 0;
}
else
{
  $src_w = $src_size_x;
  $src_h = (int)( $src_w * THUMBNAIL_SIZE_Y / THUMBNAIL_SIZE_X );
  $src_x = 0;
  $src_y = ( $src_size_y - $src_h ) / 2;
}

Comments (1) | Tags: webdev algorithms php | Author: Adam Sawicki | Share

20:16
Fri
04
Jun 2010

Smarty Cheatsheet

I knew about this before, but today I've found some time to deeply study Smarty - a free template engine for PHP. Templates in webdev (not to confuse with templates in C++) look like an interesting concept, because they separate PHP code from HTML code. I wish I used them from the start during coding my homepage and the www.gamedev.pl website instead of print()-ing HTML tags directly...

Anyway, I've written a small Smarty Cheatseet. Of course it makes sense only after studying the original library documentation, it cannot replace it.

By the way, I was also looking for some PHP library for creating RSS or ATOM feeds and I've found Feedcreator. Now I'm going to make something useful with it...

NEW: This "something" is an RSS feed for newest screenshots uploaded into www.gamedev.pl website: Feed Warsztat - Screeny, as well as for projects: Feed Warsztat - Projekty.

Comments (15) | Tags: webdev php libraries | Author: Adam Sawicki | Share

11:46
Sun
02
Aug 2009

Backup Script in PHP

One of my personal security procedures is daily backup of the code I'm working on to my FTP server. This process requires packing files to an archive and upload the archive to the FTP server. Most annoying part when doing it manually was always selecting aproppriate files, without all these big temporary files and directories like Debug, Release, .ncb, .suo, .user etc.

Today in the morning I've decided to automate this backup task with a script. As I like to use PHP as shell scripting language, here is my backup script in PHP: Backup.php_. It was not a suprise for me to see that PHP already have all the functionality I need, including building ZIP archive and FTP client. Some comments to my code:

To run the script, you must have PHP installed. Then you can just enter something like this into command line:

php Backup.php D:\MyGreatProject

Or you can create BAT file to support backup of your particular project:

@php D:\Backup.php D:\MyGreatProject
@pause

After the script have been started, you have to enter "backup name". It is the name of the ZIP file that will be uploaded to FTP. Script recursively searches for files and subdirectories in the given project directory, filters them using functions FilterDir and FilterFile, packs them to a temporary archive file and then uploads the archive to the FTP server. All remaining parameters (temporary archive path, FTP hostname, login, password and remote directory) are hardcoded as constants.

When it comes to the script code, one of the issues was to ask user about the backup name using system console. PHP has an extension called Readline, but unfortunately it doesn't work on Windows. The solution was to use fgets function with STDIN constant:

print("Enter backup name (default: \"$sSuggestedBackupName\"): ");
$sBackupName = trim(fgets(STDIN, 1024));
if (!$sBackupName)
  $sBackupName = $sSuggestedBackupName;

When it comes to building ZIP archive and handling FTP connection, PHP also has extensions for that and they seem to just work in my PHP without any problems. Zip extension exposes class ZipArchive and FTP extension exposes functions ftp_, like ftp_connect or ftp_put. All is documented in the official PHP documentation.

Comments (1) | Tags: scripts php tools | Author: Adam Sawicki | Share

21:24
Wed
21
Jan 2009

Pasek przełączania stron w PHP

Tym razem, nietypowo, napiszę o programowaniu stron WWW w PHP, a konkretnie o kwestii przełączania między stronami. Jeśli lista zarejestrowanych użytkowników, dodanych projektów, newsów czy czegokolwiek jest długa, istnieją różne rozwiązania na jej pokazywanie. Pierwsza to pokazać wszytkie (jak lista Parties na pouet.net). Druga to pokazać wszystkie na wybraną literę (jak lista Groups na pouet.net). Ta najczęściej stosowana to grupować obiekty po kilka na stronie i dać możliwość przełączania się między numerami stron.

Z kolei wybór strony też można pokazywać na różne sposoby. Można wstawić formularz z kontrolką <select>, czyli ComboBox. Można pokazać listę numerów stron. Jeśli jednak sama ta lista jest długa, warto ją skrócić. Tu właśnie, kończąc ten wstęp, chciałbym zaprezentować kawałek kodu PHP, który ostatnio napisałem. Wprowadziłem go do swojej strony domowej oraz na www.gamedev.pl. Wzorem SMF (używanego na naszym forum.gamedev.pl) pokazuje tylko numer strony bieżącej, pierwszej, ostatniej, dwóch poprzednich, dwóch następnych oraz dodatkowe strzałeczki do następnej i poprzedniej. Oto kod: Pasek_stron_PHP.txt. Tak to wygląda:

Pasek przełączania stron w PHP

Comments (1) | Tags: php webdev web gui | Author: Adam Sawicki | Share

Pages: 1

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki