我正在尝试使用imagejpeg()显示图像,但url的链接显示为图像

时间:2017-04-08 10:31:53

标签: php html browser imagejpeg

以下是我在一些合并后显示图片的代码

<?php
header('Content-type: image/jpeg');

//include 'config/db_config.php';

$img_loc="img/uploads/meme/16831967_1203762453076044_7125239636272176425_n.jpg";//$request->image;

$img_url="../".$img_loc;
    
$max_size = 800; //max image size in Pixels
$destination_folder = '../img';
$watermark_png_file = '../img/files/watermark.png'; //path to watermark image
    
$image_name = end(split('/',$img_loc));//$_FILES['file']['name']; //file name

// $image_size = $_FILES['file']['size']; //file size
// $image_temp = $_FILES['file']['tmp_name']; //file temp
// $image_type = $_FILES['file']['type']; //file type

switch(exif_imagetype($img_url)){ //determine uploaded image type 
    //Create new image from file
    case IMAGETYPE_PNG: 
        $image_resource =  imagecreatefrompng($img_url);
        break;
    case IMAGETYPE_GIF:
        $image_resource =  imagecreatefromgif($img_url);
        break;          
    case IMAGETYPE_JPEG :
        $image_resource = imagecreatefromjpeg($img_url);
        break;
    default:
        $image_resource = false;
}
    
if($image_resource){
    //Copy and resize part of an image with resampling
    list($img_width, $img_height) = getimagesize($img_url);
    
    //Construct a proportional size of new image
    $image_scale        = min($max_size / $img_width, $max_size / $img_height); 
    $new_image_width    = $img_width;//ceil($image_scale * $img_width);
    $new_image_height   = $img_height+15;//ceil($image_scale * $img_height);
    $new_canvas         = imagecreatetruecolor($new_image_width , $new_image_height);

    //Resize image with new height and width
    if(imagecopy($new_canvas, $image_resource , 0, 0, 0, 0, $new_image_width, $new_image_height))
    {
        
        if(!is_dir($destination_folder)){ 
            mkdir($destination_folder);//create dir if it doesn't exist
        }
        
        $watermark = imagecreatefrompng($watermark_png_file); //watermark image

        // Make a canvas for water logo with width same as the uploaded image
        $watermark_canvas = imagecreatetruecolor($new_image_width , 15);

        // Get width and height of watermark
        list($w_img_width,$w_img_height)=getimagesize('../img/files/watermark.png');

        // Resize water mark
        imagecopyresampled($watermark_canvas, $watermark , 0, 0, 0, 0, $new_image_width, 15, $w_img_width, $w_img_height);

        //calculate center position of watermark image
        $watermark_left = ($new_image_width/2)-(300/2); //watermark left
        $watermark_bottom = $new_image_height-15;//($new_image_height/2)-(100/2); //watermark bottom

        

        //use PHP imagecopy() to merge two images.
        imagecopy($new_canvas, $watermark_canvas,0, $watermark_bottom, 0, 0, $new_image_width, 15); //merge image
        
        //output image direcly on the browser.
        //header('Content-Type: image/pjpeg');
        //echo $new_canvas;
        imagejpeg($new_canvas);

        
        
        //Or Save image to the folder
        //imagejpeg($new_canvas, $destination_folder.'/'.$image_name , 90);
        
        
    }
}
//free up memory
// imagedestroy($new_canvas); 
// imagedestroy($image_resource);
// die();
?>

这是我在浏览器中获得的 enter image description here

当我检查此页面时,我的php的URL显示为图像

<img style="-webkit-user-select: none;background-position: 0px 0px, 10px 10px;background-size: 20px 20px;background-color: white;background-image:linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%);" src="http://localhost/tmmd/www/php/download.php">

请告诉我一个正确的显示图像的方法。 当我将此图像保存为imagejpeg($ new_canvas,$ destination_folder。'/'。$ image_name,90);此图像已正确保存

0 个答案:

没有答案