我想在根目录写入一个名为xxx.php的文件,文件内容是
$filename= $_GET['filename'];
$width = $_GET['width'];
$height = $_GET['height'];
$path="img/"; //finish in "/"4399_17315873250.jpg
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($path.$filename);
 
$dst_scale = $height/$width; //目标图像长宽比
$src_scale = $height_orig/$width_orig; // 原图长宽比
 
if ($src_scale>=$dst_scale){  // 过高
    $w = intval($width_orig);
    $h = intval($dst_scale*$w);
 
    $x = 0;
    $y = ($height_orig - $h)/2;
} else { // 过宽
    $h = intval($height_orig);
    $w = intval($h/$dst_scale);
 
    $x = ($width_orig - $w)/2;
    $y = 0;
}// 剪裁
$source=imagecreatefromjpeg($path.$filename);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped, $source, 0, 0, $x, $y, $width_orig, $height_orig);
 
// 缩放
$scale = $width / $w;
$image_p = imagecreatetruecolor($width, $height);
$final_w = intval($w * $scale);
$final_h = intval($h * $scale);
imagecopyresampled($image_p, $croped, 0, 0, 0, 0, $final_w,$final_h, $w, $h);// Output
imagejpeg($image_p, null, 100);
// Imagedestroy
imagedestroy ($image_p);
请问该怎么写啊,前台调用<img src="xxx.php?filename=photo.jpg&width=100&height=100"> 

解决方案 »

  1.   

    <img src="<?= $img_path?>">  
    吧生成的图片路径直接传到html里面就可以了
      

  2.   

    不是这个意思啊,我是想通过php在根目录创建并写入上面的php代码
      

  3.   


    $myfile = fopen("xxx.php", "w") or die("Unable to open file!");
    $txt = "xxxxxxxxx";
    fwrite($myfile, $txt);
    fclose($myfile);
    这样可以直接生成一个文件
      

  4.   

    谢谢,但是写入上面的php代码报错,也就是说$txt = "xxxxxxxxx";中间的xxxxxxxxx替换为php的代码报错
      

  5.   

    $s =<<< 'TXT'
    $filename= $_GET['filename'];
    $width = $_GET['width'];
    $height = $_GET['height'];
    $path="img/"; //finish in "/"4399_17315873250.jpg
    // Content type
    header('Content-type: image/jpeg');
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($path.$filename);
     
    $dst_scale = $height/$width; //目标图像长宽比
    $src_scale = $height_orig/$width_orig; // 原图长宽比
     
    if ($src_scale>=$dst_scale){  // 过高
        $w = intval($width_orig);
        $h = intval($dst_scale*$w);
     
        $x = 0;
        $y = ($height_orig - $h)/2;
    } else { // 过宽
        $h = intval($height_orig);
        $w = intval($h/$dst_scale);
     
        $x = ($width_orig - $w)/2;
        $y = 0;
    }// 剪裁
    $source=imagecreatefromjpeg($path.$filename);
    $croped=imagecreatetruecolor($w, $h);
    imagecopy($croped, $source, 0, 0, $x, $y, $width_orig, $height_orig);
     
    // 缩放
    $scale = $width / $w;
    $image_p = imagecreatetruecolor($width, $height);
    $final_w = intval($w * $scale);
    $final_h = intval($h * $scale);
    imagecopyresampled($image_p, $croped, 0, 0, 0, 0, $final_w,$final_h, $w, $h);// Output
    imagejpeg($image_p, null, 100);
    // Imagedestroy
    imagedestroy ($image_p);
    TXT
    file_put_contents('xxx.php', $s);
      

  6.   

    <?php
    $s =<<< 'TXT'
    $width = $_GET['width'];
    $height = $_GET['height'];
    $path="img/"; //finish in "/"4399_17315873250.jpg
    // Content type
    header('Content-type: image/jpeg');
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($path.$filename);
     
    $dst_scale = $height/$width; //目标图像长宽比
    $src_scale = $height_orig/$width_orig; // 原图长宽比
     
    if ($src_scale>=$dst_scale){  // 过高
        $w = intval($width_orig);
        $h = intval($dst_scale*$w);
     
        $x = 0;
        $y = ($height_orig - $h)/2;
    } else { // 过宽
        $h = intval($height_orig);
        $w = intval($h/$dst_scale);
     
        $x = ($width_orig - $w)/2;
        $y = 0;
    }// 剪裁
    $source=imagecreatefromjpeg($path.$filename);
    $croped=imagecreatetruecolor($w, $h);
    imagecopy($croped, $source, 0, 0, $x, $y, $width_orig, $height_orig);
     
    // 缩放
    $scale = $width / $w;
    $image_p = imagecreatetruecolor($width, $height);
    $final_w = intval($w * $scale);
    $final_h = intval($h * $scale);
    imagecopyresampled($image_p, $croped, 0, 0, 0, 0, $final_w,$final_h, $w, $h);// Output
    imagejpeg($image_p, null, 100);
    // Imagedestroy
    imagedestroy ($image_p);
    TXT
    file_put_contents('xxx.php', $s);
    ?>
     从第三行开始报错
      

  7.   

    报什么错?你的 php 版本是多少?
      

  8.   

    php版本5.6
      

  9.   

    基本不懂php,请版主告之一下完整代码