アプリ版:「スタンプのみでお礼する」機能のリリースについて

<?php
// 空の画像を作成し、テキストを追加します
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

// content type ヘッダを、ここでは image/jpeg と設定します
header('Content-type: image/jpeg');

// 画像を出力します
imagejpeg($im);

// メモリを開放します
imagedestroy($im);
?>

http://www.php.net/manual/ja/function.imagejpeg. …

を参照して(そのまま利用して)、画像を作成して表示させてみました。
すると、これはその通り、うまく画像が表示されました。

しかし、

<?php
// 空の画像を作成し、テキストを追加します
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

// 【追加分】: 画像を保存する
imagejpeg($im, 'new.jpg');

// content type ヘッダを、ここでは image/jpeg と設定します
header('Content-type: image/jpeg');

// 画像を出力します
imagejpeg($im);

// メモリを開放します
imagedestroy($im);
?>

のように

imagejpeg($im, 'new.jpg');

を挿入して、その画像を保存しようとしているのですが、うまくいきません。(何も起こりません。)
ちなみに、保存するためのディレクトリのパーミッションはその親ディレクトリを含め、(7,7,7)です。

「new.jpg(相対パス)」を「/www/.../new.jpg」のように絶対パスを使ってやっても結果は同じでうまくいきませんでした。
どのようにすれば、新しく作成した画像を保存できるのでしょうか?

ちなみに、OSはLinux(Fedora10)です。もちろん、GDは先のテストで証明される通り、動作していると思います。

よろしくお願い致します。

A 回答 (2件)

当方の環境では問題なく動作(保存と表示)しました。


何かエラーは出ていないのでしょうか。エラーが表示されない(or残らない)設定になっているのであればそれを変更してみてください。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

次のようなエラーが出ます.

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'new.jpg' for writing: Permission denied in /var/.../index.php on line 15

書きました通り、当ディレクトリのパーミッションは(7,7,7)です。試験的に「/var/.../new.jpg」を作成して、パーミッションを(7,7,7)に設定してやってみましたが、
上記と同様のエラーが表示されました。

お礼日時:2009/12/21 00:01

ANo.1です。



エラーの行を見る限り例示されているコードとは違うコードの実行結果のようなので不明な部分もありますが、書き込み権限がないというエラーであることは確かですね。
パーミッションの設定そのものや設定しているディレクトリ自体が正しいか確認するくらいでしょうか。
あとは、PHPの特定のバージョンにおいて、セーフモードの場合にimagejpeg()では一切ファイルを作成できないバグがあるようです。

この回答への補足

ちなみに、php.iniの一部をここに記します。

;
; Safe Mode
;
safe_mode = Off

; By default, Safe Mode does a UID compare check when
; opening files. If you want to relax this to a GID compare,
; then turn on safe_mode_gid.
safe_mode_gid = Off

; When safe_mode is on, UID/GID checks are bypassed when
; including files from this directory and its subdirectories.
; (directory must also be in include_path or full path must
; be used when including)
safe_mode_include_dir =

; When safe_mode is on, only executables located in the safe_mode_exec_dir
; will be allowed to be executed via the exec family of functions.
safe_mode_exec_dir =

; Setting certain environment variables may be a potential security breach.
; This directive contains a comma-delimited list of prefixes. In Safe Mode,
; the user may only alter environment variables whose names begin with the
; prefixes supplied here. By default, users will only be able to set
; environment variables that begin with PHP_ (e.g. PHP_FOO=BAR).
;
; Note: If this directive is empty, PHP will let the user modify ANY
; environment variable!
safe_mode_allowed_env_vars = PHP_

; This directive contains a comma-delimited list of environment variables that
; the end user won't be able to change using putenv(). These variables will be
; protected even if safe_mode_allowed_env_vars is set to allow to change them.
safe_mode_protected_env_vars = LD_LIBRARY_PATH

たぶん、セーフモードにはなっていないと思います。

補足日時:2009/12/23 21:10
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。
お返事が遅れて失礼致します。

前にも書きました通り、パーミッションは(7,7,7)です。
今度はPHPのセーフモードについて考えてみたいと思います。

お礼日時:2009/12/23 21:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!