 |
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
 | This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 27th, 2013, 05:30 AM
|
|
Registered User
|
|
Join Date: Jul 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 7, preview does not work
Hi, I have problem with codes from chapter 7. Everythnig works fine, except preview. When I choose some filter and click on preview, the image dissapears. But when I save the image, everything is ok, filter is active.
Here are my codes:
chceck_image.php
Code:
<?php
// pøipojenà k databázi MySQL
$db = mysql_connect('localhost', 'root', '') or
die ('Nemohu se pøipojit. Zkontrolujte prosÃm pøipojenà k serveru.');
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// Upravte cestu tak, aby odpovÃdala vaÅ¡emu umÃstìnÃ.
$dir ='A:/www/obrazky';
// Upravte cestu tak, aby odpovÃdala vaÅ¡emu umÃstìnÃ.
putenv('GDFONTPATH=C:/Windows/Fonts');
$font = 'C:/Windows/Fonts/arial.ttf';
// Obsluha pøenášených souborù.
if ($_POST['submit'] == 'Odeslat') {
// Nejprve se ujistìte, že pøenos probìhl v poøádku.
if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch ($_FILES['uploadfile']['error']) {
case UPLOAD_ERR_INI_SIZE:
die('Velikost pøenášeného souboru pøesáhla hodnotu ' .
'upload_max_filesize definovanou v souboru php.ini.');
break;
case UPLOAD_ERR_FORM_SIZE:
die('Velikost pøenášeného souboru pøesáhla hodnotu MAX_FILE_SIZE ' .
'urèenou ve formuláøi HTML.');
break;
case UPLOAD_ERR_PARTIAL:
die('Soubor byl pøenesen jen èásteènì.');
break;
case UPLOAD_ERR_NO_FILE:
die('Nebyl pøenesen žádný soubor.');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('Server nemá k dispozici doèasný adresáø.');
break;
case UPLOAD_ERR_CANT_WRITE:
die('Serveru se nepodaøilo uložit pøenesený soubor na disk.');
break;
case UPLOAD_ERR_EXTENSION:
die('Pøenos souboru zastaven doplòkem PHP.');
break;
}
}
// Zjištìnà informacà o pøeneseném souboru.
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = @date('Y-m-d');
list($width, $height, $type, $attr) =
getimagesize($_FILES['uploadfile']['tmp_name']);
// Zkontrolujte, zda podporujete formát pøeneseného souboru.
$error = 'Formát pøeneseného souboru nenà podporován.';
switch ($type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or
die($error);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or
die($error);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or
die($error);
break;
default:
die($error);
}
// Uložte informace do tabulky.
$query = 'INSERT INTO images (image_caption, image_username, image_date)
VALUES ("' . $image_caption . '", "' . $image_username . '", "'
. $image_date . '")';
$result = mysql_query($query, $db) or die (mysql_error($db));
// Naètenà identifikátoru obrázkù generovaného databázà MySQL automaticky
// pøi vloženà nového záznamu.
$last_id = mysql_insert_id();
// Uloženà obrázku na koneèné mÃsto.
$image_id = $last_id;
imagejpeg($image, $dir . '/' . $image_id . '.jpg');
imagedestroy($image);
} else {
// Zjištìnà informacà o obrázku.
$query = 'SELECT
image_id, image_caption, image_username, image_date
FROM
images
WHERE
image_id = ' . $_POST['id'];
$result = mysql_query($query, $db) or die (mysql_error($db));
extract(mysql_fetch_assoc($result));
list($width, $height, $type, $attr) = getimagesize($dir . '/' . $image_id .
'.jpg');
}
if ($_POST['submit'] == 'Uložit') {
// Ovìøte platnost obrázku.
if (isset($_POST['id']) && ctype_digit($_POST['id']) &&
file_exists($dir . '/' . $_POST['id'] . '.jpg')) {
$image = imagecreatefromjpeg($dir . '/' . $_POST['id'] . '.jpg');
} else {
die('Zadán neplatný obrázek.');
}
// Aplikace filtru.
$effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
switch ($effect) {
case IMG_FILTER_NEGATE:
imagefilter($image, IMG_FILTER_NEGATE);
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image, IMG_FILTER_GRAYSCALE);
break;
case IMG_FILTER_EMBOSS:
imagefilter($image, IMG_FILTER_EMBOSS);
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
break;
}
// Požaduje-li to uživatel, pøidejte popisek.
if (isset($_POST['emb_caption'])) {
imagettftext($image, 12, 0, 20, 20, 0, $font, $image_caption);
}
// Uložte obrázek po aplikaci filtru.
imagejpeg($image, $dir . '/' . $_POST['id'] . '.jpg', 100);
?>
<html>
<head>
<title>Zde je váš obrázek!</title>
</head>
<body>
<h1>Obrázek byl úspìšnì uložen!</h1>
<img src="obrazky/<?php echo $_POST['id']; ?>.jpg" />
</body>
</html>
<?php
} else {
?>
<html>
<head>
<title>Zde je váš obrázek!</title>
</head>
<body>
<h1>Takže, jaké to je, být slavný?</h1>
<p>Zde je obrázek, který jste právì odeslali na náš server:</p>
<?php
if ($_POST['submit'] == 'Odeslat') {
$imagename = 'obrazky/' . $image_id . '.jpg';
} else {
$imagename = 'image_effect.php?id=' . $image_id . '&e=' .
$_POST['effect'];
if (isset($_POST['emb_caption'])) {
$imagename .= '&capt=' . urlencode($image_caption);
}
}
?>
<img src="<?php echo $imagename; ?>" style="float:left;">
<table>
<tr><td>Obrázek je uložen jako: </td>
<td><?php echo $image_id . '.jpg'; ?></td></tr>
<tr><td>Výška: </td><td><?php echo $height; ?></td></tr>
<tr><td>Å Ãøka: </td><td><?php echo $width; ?></td></tr>
<tr><td>Datum uloženÃ: </td><td><?php echo $image_date; ?></td></tr>
</table>
<p>Obrázek mùžete upravit nìkolika zpùsoby. Poznámka: Po uloženà se zmìny
<em>stanou nevratné</em>.</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $image_id;?>"/>
Filtr: <select name="effect">
<option value="-1">Žádný</option>
<?php
echo '<option value="' . IMG_FILTER_GRAYSCALE . '"';
if (isset($_POST['effect']) && $_POST['effect'] ==
IMG_FILTER_GRAYSCALE) {
echo ' selected="selected"';
}
echo '>ÃernobÃlý</option>';
echo '<option value="' . IMG_FILTER_GAUSSIAN_BLUR . '"';
if (isset($_POST['effect']) && $_POST['effect'] ==
IMG_FILTER_GAUSSIAN_BLUR) {
echo ' selected="selected"';
}
echo '>Rozmazat</option>';
echo '<option value="' . IMG_FILTER_EMBOSS . '"';
if (isset($_POST['effect']) && $_POST['effect'] ==
IMG_FILTER_EMBOSS) {
echo ' selected="selected"';
}
echo '>Reliéf</option>';
echo '<option value="' . IMG_FILTER_NEGATE . '"';
if (isset($_POST['effect']) && $_POST['effect'] ==
IMG_FILTER_NEGATE) {
echo ' selected="selected"';
}
echo '>Negativ</option>';
?>
</select>
<br/><br/>
<?php
echo '<input type="checkbox" name="emb_caption"';
if (isset($_POST['emb_caption'])) {
echo ' checked="checked"';
}
echo '>Vložit do obrázku popisek?';
?>
<br/><br/>
<input type="submit" value="Náhled" name="submit" />
<input type="submit" value="Uložit" name="submit" />
</div>
</form>
</body>
</html>
<?php
}
?>
image_effect.php
Code:
<?php
// Upravte cestu tak, aby odpovÃdala vaÅ¡emu umÃstìnÃ.
$dir ='A:/www/obrazky';
// Zmìòte cestu tak, aby odpovÃdala umÃstìnà souborù pÃsem ve vaÅ¡em systému.
// PøÃpadnì vyberte jiné pÃsmo.
putenv('GDFONTPATH=C:/Windows/Fonts');
$font = 'C:/Windows/Fonts/arial.ttf';
// Ovìøte, že obrázek je platný.
if (isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir . '/' .
$_GET['id'] . '.jpg')) {
$image = imagecreatefromjpeg($dir . '/' . $_GET['id'] . '.jpg');
} else {
die('Zadán neplatný obrázek.');
}
// Použijte filtr.
$effect = (isset($_GET['e'])) ? $_GET['e'] : -1;
switch ($effect) {
case IMG_FILTER_NEGATE:
imagefilter($image, IMG_FILTER_NEGATE);
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image, IMG_FILTER_GRAYSCALE);
break;
case IMG_FILTER_EMBOSS:
imagefilter($image, IMG_FILTER_EMBOSS);
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
break;
}
// Na vyžádánà pøidejte popisek.
if (isset($_GET['capt'])) {
imagettftext($image, 12, 0, 20, 20, 0, $font, $_GET['capt']);
}
// Zobrazenà obrázku.
header('Content-Type: image/jpeg');
imagejpeg('$image', '', 100);
?>
upload_image.html
Code:
<html>
<head>
<title>Pošlete obrázek na náš web!</title>
<style type="text/css">
<!--
td {vertical-align: top;}
-->
</style>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Uživatelské jméno</td>
<td><input type="text" name="username" /></td>
</tr><tr>
<td>Odeslat obrázek:</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="262144"/>
<input type="file" name="uploadfile" />
</td>
</tr><tr>
<td colspan="2">
<small><em>* PřÃpustné formáty obrázků: GIF, JPG/JPEG a PNG.
</em></small>
</td>
</tr><tr>
<td>Název nebo popisek obrázku<br/>
</td>
<td><input type="text" name="caption" /></td>
</tr><tr>
<td colspan="2" style="text-align: center">
<input type="submit" name="submit" value="Odeslat"/>
</td>
</tr>
</table>
</form>
</body>
</html>
It is in Czech language but I hope that you will be understand the main part of code.
Thanks, samson.
|
|

October 27th, 2013, 12:09 PM
|
|
Registered User
|
|
Join Date: Oct 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think I got the answer, in image_effect.php at the bottom of the code:
PHP Code:
// show the image header('Content-Type: image/jpeg'); imagejpeg($image, '', 100); ?>
Should be:
PHP Code:
// show the image header('Content-Type: image/jpeg'); imagejpeg($image, NULL, 100); ?>
Because we can't leave this second parameter filename as '' instead use NULL and try again uploading an image.
Hope it works :)
Last edited by register_null; October 27th, 2013 at 12:16 PM..
|
|
 |